题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <iostream>
#include <string>
using namespace std;
int main() {
string inp;
getline(cin, inp);
int n = inp.size();
int j = 0;
int i = 0;
for (i = 0; i <= n; i++) {
if (i == n) break;
if (inp[i] == ' ') {
j = i + 1;
}
}
cout << i - j;
return 0;
}
// 64 位输出请用 printf("%lld")
#华为OD机试真题#华为OD机测试题 文章被收录于专栏
个人练习专栏
查看12道真题和解析