码迷,mamicode.com
首页 > 其他好文 > 详细

Length of Last Word

时间:2014-11-05 21:11:57      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   div   on   

比较简单的问题:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int lengthOfLastWord(const char *s);
    char a[20];
    cin.getline(a,20);
    cout << lengthOfLastWord(a) << endl;
}

int lengthOfLastWord(const char *s) {
        int i=0,new_word=0,last_length=0;
        while(s[i]!=\0){
            if(s[i]== )
                new_word=1;
            else if(new_word==1 && s[i]!= ){
                last_length=1;
                new_word=0;
            }
            else{
                ++last_length;
            }
            ++i;
        }
        return last_length;
    };

 

Length of Last Word

标签:style   blog   io   color   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/iois/p/4077196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!