我目前正在为大学项目工作,我有以下功能,它接收一行(字符串)并将其分为单词:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我想打印字符串"token"的第一个字母,但是它打印整个字符串,每当我使用其他方法(*token,token [0])时,它会创建一个错误,指出%s期望类型*char而不是int.我如何只打印字符串的第一个字母,以便将来在我的代码中实现?
非常简单:
printf("%c\n", *token);
Run Code Online (Sandbox Code Playgroud)
要么
printf("%c\n", token[0]);
Run Code Online (Sandbox Code Playgroud)