gpt4 book ai didi

C - 使用指针打印字符串的第一个字母

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:03 24 4
gpt4 key购买 nike

我目前正在做一个大学项目,我有以下功能,它接收一行(字符串)并将其分成单词:

static const char separators[] = {' ','\t',',',';','.','?','!','"','\n',':','\0'}; 

void split(char *line){
char *token = strtok(line, separators);
while(token!=NULL) {
strtolower(token);
printf("%s \n", token);
/* rest of code belongs here */
token = strtok(NULL, separators);
}
}

出于测试目的,我想打印字符串 token 的第一个字母,但是它会打印整个字符串,每当我使用其他方法时 (*token, token[0]) 它会创建一个错误,指出 %s 需要类型 *char 而不是 int

我如何才能只打印字符串的第一个字母,以便将来在我的代码中实现?

最佳答案

非常简单:

printf("%c\n", *token);

printf("%c\n", token[0]);

关于C - 使用指针打印字符串的第一个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37034939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com