题解 | 字符串最后一个单词的长度
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <iostream>
#include<string>
using namespace std;
//倒着来就行了,不计入空格
int main() {
string s;
while(getline(cin,s)){
int n = s.length()-1;
while(s[n]==' '){
n--;
}
int count = 0;
for(n;n>=0;n--){
if(s[n]==' ') break;
count++;
}
cout<<count<<endl;
}
return 0;
}
查看12道真题和解析