Given astrings consists of upper/lower-casealphabetsandempty space characters' ',returnthe length of last word in the string.
If the last word doesnotexist,return0.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: "Hello World"
Output:
5
求句末单词的长度
代码:
class Solution {
public:
int lengthOfLastWord(string s) {
int right=s.length()-1,res=0;
if(s.length()==0)
return 0;
while(right>=0 && s[right]==' ')
{
right--;
}
while (right>=0 && s[right]!=' ') {
res++;
right--;
}
return res;
}
};