题解 | #字符串分隔#

字符串最后一个单词的长度

http://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da

c++简洁代码

mark一下他人写法

#include<bits/stdc++.h>
using namespace std;

int main() {
    string s;
    while(cin >> s);
    cout << s.size();
    return 0;
}

自己写法

#include<iostream>
using namespace std;
int main(){
    string sentence;
    while(getline(cin, sentence)){
        while(sentence != ""){
            int position = sentence.find(' ');
            if(position != sentence.npos){
                sentence.erase(0, position+1);
            }else{
                cout << sentence.length() << endl;
                break;
            }
        }
    }
}
全部评论

相关推荐

09-30 14:33
Python
点赞 评论 收藏
分享
牛牛不会牛泪:脉脉太多这种了,纯水军
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务