题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <stdio.h>
#include"string.h"
int main() {
int a=0, b=-1;
char str[5000];
gets( str);
while (str[a]!='\0') { // 注意 while 处理多个 case
// 64 位输出请用 printf("%lld") to
if(str[a]==' ')
{
b=a;
}
a++;
}
printf("%d\n", strlen(str)-b-1);
return 0;
}
读取最后一个空格的下标,然后通过strlen函数读取字符串的长度,两者相减再稍加调试即可
查看20道真题和解析