题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
将字符串读取到字符数组str中,然后遍历每一个数组元素,若元素非空字符则计数一,反之将计数变量归0,直到最后一个单词。
#include<stdio.h>
#include<string.h>
int main()
{
char str[5000]={0};
scanf("%[^\n]",str);
int len=strlen(str);
int num=0;
for(int i=0;i<len;i++)
{
if(str[i]!=' ') num++;
else num=0;
}
printf("%d",num);
return 0;
}



