天天看点

【华为机试】字符串最后一个单词的长度

#题目描述:

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

#输入描述:

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

#输出描述:

整数N,最后一个单词的长度。
示例1
输入
hello world
输出
5
           

#参考程序:

#include <iostream>
#include <string>
using namespace std;
int main(){	
	string str;
	getline(cin,str);
	int count = str.size()-1;
	int i=0;
	for(i=count;i>=0;--i)
		if(str[i]==' ')break;
	if(i==0)
		cout<<count+1<endl;
	else
		cout<<count-i<<endl;
}