题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
//https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da?tpId=37&tqId=21224&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26pageSize%3D50%26search%3D%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title= #include <iostream> using namespace std; int main() { string s; while (getline(cin, s)) { int pos = -1; for(int i = 0;i<s.size();i++) if(s[i]==' ') //string的元素为字符型 pos = i; cout << s.size()-pos-1 << endl; } }