题目:计算字符串最后一个单词的长度,单词以空格隔开
#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;
}