题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
getline(cin, str);
int a = str.length();
int b;
for (int i = 0; i < a; ++i) {
if (i == 0 && str[i] !=' ') {
b=1;
}
else if (i == 0 && str[i] == ' ') {
b=0;
}
else if (str[i] != ' ' && str[i - 1] != ' ') {
b+=1;
}
else if(str[i] != ' ' && str[i - 1] == ' ') {
b=1;
}
}
cout << b << endl;
}
