[LeetCode58] Length of Last Word

题目:

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

分类:String

代码:

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(string s) {
 4         string word;
 5         string res;
 6         stringstream ss(s);
 7         while(!ss.eof())
 8         {
 9             ss >> word;
10             res = word;
11         }
12         if(res.empty())
13             return 0;
14         else
15             return res.size();
16     }
17 };

 

posted @ 2016-08-11 10:33  zhangbaochong  阅读(194)  评论(0)    收藏  举报