题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <stdio.h>
int main() {
char c;
int count=0;
//while(scanf("%c",&c)!='\n') 这是错误代码,scanf返回值与c本身的值本就不匹配
//或者还可以改造成while(scanf("%c",&c)&&c!='n')以简化下面的两行代码
while (scanf("%c", &c)) {
if('\n'==c) break;
if(' '==c)
count=0;
else{
count++;
}
}
printf("%d\n",count);
return 0;
}
查看29道真题和解析