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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/soeben/article/details/79619017

题目描述:

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


输入描述:

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


输出描述:

整数N,最后一个单词的长度。

示例1
输入
hello world
输出
5

参考程序:

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

猜你喜欢

转载自blog.csdn.net/soeben/article/details/79619017