原题地址
简单模拟题
代码:
1 int lengthOfLastWord(const char *s) { 2 char prev = ' '; 3 int length = 0; 4 5 while (*s) { 6 if (*s != ' ') { 7 if (prev == ' ') 8 length = 1; 9 else 10 length++; 11 } 12 prev = *s; 13 s++; 14 } 15 16 return length; 17 }