获取最后一个单词长度
字符串最后一个单词的长度
http://www.nowcoder.com/questionTerminal/8c949ea5f36f422594b306a2300315da
#include <string>
#include <iostream>
using namespace std;
class Solution {
public:
int CountWordLen(string str) {
int len = str.size();
if(len == 0) return 0;
int count = 0;
for(int i = len-1; i>=0 && str[i] != ' '; i--) {
count++;
}
return count;
}
};
int main() {
Solution S;
string str;
while(getline(cin,str)) {
cout << S.CountWordLen(str) << endl;
}
return 0;
}
阿里云成长空间 747人发布