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 };

 

posted on 2013-05-13 03:52  tanghulu321  阅读(98)  评论(0编辑  收藏  举报