Length of Last Word
2015-04-11 11:16 笨笨的老兔子 阅读(136) 评论(0) 编辑 收藏 举报一个字符串由单词和空格组成,求最后一个单词的长度
注意点:小心最后有一串空格的样例
class Solution {
public:
int lengthOfLastWord(const char *s) {
int length = 0;
int i = strlen(s) - 1;
while (i>=0 &&s[i] == ' ')
i--;
while(i>= 0&&s[i]!=' ')
{
i--; length++;
}
return length;
}
};