题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <iostream> #include <string> #include<stack> using namespace std; int main() { string s1; getline(cin,s1); stack<int> IndexSt; IndexSt.push(0); int i=0; while(i<s1.length()) { if(s1[i] == ' ') { IndexSt.push(i+1); } ++i; } int ret = 0; int last_index = IndexSt.top(); while(last_index < s1.length()) { ++ret; ++last_index; } cout<<ret<<endl; return 0; } // 64 位输出请用 printf("%lld")