题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于5000。
输出描述:
整数N,最后一个单词的长度。
示例1
输入
hello world
输出
5
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string in;
int count=0;
while(getline(cin,in))
{
int i=0;
while(in[i]!='\0')
{
if(in[i]==' ') //不可为 in[i++]
count=0;
else
count++;
i++;
}
}
cout<<count;
return 0;
}
此题参考他人答案!