题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string str = "";
std::getline(cin, str);
int iLen = str.length();
if (iLen >= 5000) {
str = str.substr(0, 4999);
}
int iBegin = -1, iEnd = -1;
for (int i = iLen - 1; i >=0; i--) {
if (str[i] != ' ' && iEnd < 0) {
iEnd = i;
}
if (iEnd >= 0 && str[i] == ' ') {
iBegin = i;
break;
}
}
std::cout << iEnd - iBegin << std::endl;
return 0;
}
// 64 位输出请用 printf("%lld")

