class Solution { public: int lengthOfLastWord(string s) { int i=0; int len=0; while(s[i]&&s[i]==' ') i++; //去掉开头的空格 bool flag = false; while(s[i]){ cout<<len<<endl; if(s[i]!=' '){ if(flag == true) //如果要计算下一个word,那么len=1 {len=1;flag=false;} else //如果还是当前word,len++ len++; } else flag=true; //是空格表示接下来,要统计的另外一个word了 i++; } return len; } };