题解 | 字符串最后一个单词的长度
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
#include <iostream> using namespace std; int main() { string mystr; getline(cin, mystr); // 读取整行 cin遇到空格就停止了 int n = 0; for(char a:mystr) { //遍历字符串 if(a != ' '){ n++; } else{ n = 0; //遇到空格直接置零 } } cout << n << endl; } // 64 位输出请用 printf("%lld")