给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度。
如果不存在最后一个单词,请返回 0 。
说明:一个单词是指由字母组成,但不包含任何空格的字符串。
示例:
输入: "Hello World"
输出: 5
class Solution {
public:
int lengthOfLastWord(string s);
};
int Solution::lengthOfLastWord(string s)
{
string temp,Temp;
int count=s.length();
if(count==0) return 0;
for(int i=0;i<count;i++)
{
if(s[i]==' ')
{
temp="\0";
}
else
{
temp=temp+s[i];
if(temp.length()>0)
{
Temp=temp;
}
}
}
int R=Temp.length();
return R;
}