您的位置:首页 > 其它

华为机试——字符串最后一个单词长度

2016-01-08 15:25 183 查看

题目:计算字符串最后一个单词的长度,单词以空格隔开

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str,lastword;
getline(cin,str);
string::size_type pos,end=str.size()-1;
pos=str.rfind(" ",end);
if(pos!=0)
{
lastword=str.substr(pos+1,end-pos);
cout<<lastword.size();
}
else
return -1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: