题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <stdio.h> int main() { char a[5000] = {0}; int count_space = 0; gets(a); //遍历整个字符串 for(int i=0;i < strlen(a);i++) { //统计空格数量 if( a[i] == ' ') count_space ++; } //printf("count_space is %d\r\n", count_space); int j = 0; while(count_space != 0) { if(a[j] == ' ') { count_space --; } j++; } //从最后一个空格开始统计单词长度 //这里j的位置到了最后的一个空格的下一位, printf("%d", strlen(a)-j); return 0; }#华为OD#