题解 | 字符串最后一个单词的长度
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
string sentence;
getline(cin, sentence);
int end = sentence.length()-1;
// cout<<end;
if (end > 1000)
return 0;
for(int i = end;i>=0;i--)
{
if(i==0)
{
cout<<end-i+1<<endl;
}
if (sentence[i]== ' ')
{
cout<<end-i<<endl;
break;
}
}
}

查看7道真题和解析