题解 | 字符串最后一个单词的长度
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <stdio.h>
#include "string.h"
int main()
{
char str[10001];
fgets(str,sizeof(str),stdin);
int ans =0 ;
int n = strlen(str);
if(n > 0 &&str[n-1] == '\n')
{
str[n-1] = '\0';
}
for(int i = 0 ; str[i] !='\0'; i++)
{
if(str[i] != ' ')
{
ans++;
}
else
{
if (str[i+1] != '\0')
ans = 0;
}
}
printf("%d",ans);
return 0;
}


查看15道真题和解析