[華為OJ--C++]001-字符串最後一個單詞的長度。本站提示廣大學習愛好者:([華為OJ--C++]001-字符串最後一個單詞的長度)文章只能為提供參考,不一定能成為您想要的結果。以下是[華為OJ--C++]001-字符串最後一個單詞的長度正文
題目描述:計算字符串最後一個單詞的長度,單詞以空格隔開。
輸入描述:一行字符串
輸出描述:整數N,最後一個單詞的長度。
輸入例子:hello world
輸出例子:5
算法實現:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
getline(cin,str);
int count=0;
for(int i=str.length()-1;i>=0;i--)
{
if(str[i]==' ')
break;
count++;
}
cout<<count<<endl;
return 0;
}