Length of last word

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(const char *s) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         if (*s == NULL) return 0;
 7         
 8         int length = 0;
 9         int result = 0;
10         
11         while (*s){
12             
13             if (*s == ' ') {
14                 length = 0;
15                 s++;
16             }
17             
18             else {
19                 length++;
20                 result = length;
21                 s++;
22             }
23         }
24         
25         return result;
26     }
27 };
原文地址:https://www.cnblogs.com/tanghulu321/p/3074987.html