题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
int firstWorld(string s)
{
int count = 0;
int pos = s.rfind(" ");
if (pos != string::npos)
{
int len = s.length();
for (int i = len - 1; i > pos; i--)
{
++count;
}
return count;
}
else
{
int len = s.length();
return len;
}
}
};
int main()
{
Solution sol;
string s;
getline(cin,s);
int a = sol.firstWorld(s);
cout << a << endl;
return 0;
}