题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <stdio.h>
#include <string.h>
#include <stddef.h>
int main() {
char str[5002];
while (fgets(str, sizeof(str), stdin) != NULL) {
str[strcspn(str,"\n")] = '\0';
int lengthofstr = strlen(str);
char *last_space = strrchr(str, ' ');
if(last_space != NULL) {
ptrdiff_t lastwordLength = (char *)str + lengthofstr - last_space - 1;
printf("%td", lastwordLength);
} else {
size_t lastwordLength = strlen(str);
printf("%zu", lastwordLength);
}
}
return 0;
}

