题解 | 字符串最后一个单词的长度
#include<bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); int l = s.size(), sum = 0; for (int i = l - 1; i >= 0; i--) { if (s[i] != ' ') sum++; else break; } cout << sum; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); int l = s.size(), sum = 0; for (int i = l - 1; i >= 0; i--) { if (s[i] != ' ') sum++; else break; } cout << sum; return 0; }
相关推荐