来源:牛客网 字符串最后一个单词的长度
题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于5000。
输出描述:
整数N,最后一个单词的长度。
输入例子:
hello world
输出例子:
5
参考代码
#include <iostream>
#include <string.h>
using namespace std;
char str[2000];
int main(){
cin.getline(str,2000);
int len=strlen(str);
int ans=0;
while (str[len-1]!=' '&&len>0){
len--;
ans++;
}
cout<<ans<<endl;
return 0;
}