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