題目描述 計算字符串最后一個單詞的長度,單詞以空格隔開。
輸入描述: 一行字符串,非空,長度小于5000。
輸出描述: 整數N,最后一個單詞的長度。
輸入例子: hello world
輸出例子: 5
注意事項 1、應用兩個函數rfind和substr rfind:反向查找, substr:從當前位置向后的字符串 代碼段
#include<iostream>#include<string>using namespace std;int lenth(string s) { string file; while(s.size()==0) break; unsigned int f=s.rfind(' '); file=s.substr(f+1); int n=file.size(); return n; }int main() { string input; while(getline(cin,input)) { cout<<lenth(input)<<endl; } return 0;}新聞熱點
疑難解答