题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
//先读入字符串
getline(cin, str);
//然后使用rfind函数得到逆序查找倒数第一个空格
size_t pos = str.rfind(' ');
//使用size - (pos+1) 的值就是长度
cout << str.size() - (pos + 1) << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
查看9道真题和解析