题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于5000。
输出描述:
整数N,最后一个单词的长度。
示例1
输入
hello world
输出
5
#include <string>
#include <iostream>
#include <cstring>
int main()
{
std::string str;
getline(std::cin, str);
str.erase(str.find_last_not_of(" ") + 1);
int count=0;
int len=str.size();
for(int i=len-1;i>=0;i--)
{if(str[i]!=' ')
count++;
else
break;
}
std::cout<<count<<std::endl;
}
934

被折叠的 条评论
为什么被折叠?



