Leetcode#58 Length of Last Word

原题地址

 

简单模拟题

 

代码:

 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 }

 

posted @ 2015-02-02 09:23  李舜阳  阅读(147)  评论(0编辑  收藏  举报