给定由大写,小写字母和空格组成的字符串,返回 最后 一个单词的长度。
如果输入中不存在单词,返回 000。
注意:
“单词”是指不包含空格符号的字符串
例如:
对于字符串"hello World"
(不带引号), 那么返回的结果是 555;
对于字符串"abc abc "
(不带引号),那么返回的结果就是 333。
输入格式
输入仅一行,为字符串 sss(长度不超过 100001000010000)。
输出格式
输出 sss 中最后一个单词的长度。
样例输入1
Today is a nice day
样例输出1
3
样例输入2
The quick brown fox jumps over the lazy dog
样例输出2
3
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i=0,j=0;
string line;
getline(cin,line);
for (auto it=line.rbegin();it!=line.rend();it++){
if (*it==' ')j++,*it='a';
else break;
}
for (auto it=line.begin();it!=line.end();it++){
if (*it==' ') i=0;
else if (*it!=' ')
i++;
}
cout<<i-j<<endl;
return 0;
}