题目链接
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da?tpId=37&tqId=21224&tPage=1&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking
题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于5000。
输出描述:
整数N,最后一个单词的长度。
示例1
输入
复制
hello world
输出
复制
5
题解:
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
int count = 0;
getline(cin, str);
for(int i = str.length()-1; i >= 0; i--){
if(str[i] != ' ')
count++;
else
break;
}
cout << count << endl;
return 0;
}