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

题目描述

计算字符串最后一个单词的长度,单词以空格隔开。

输入描述:

一行字符串,非空,长度小于5000。

输出描述:

整数N,最后一个单词的长度。

示例1

输入

复制

hello world

输出

复制

5


#include <iostream>

#include <string>

#include <vector>

using namespace std;

int main()

{

    string strIn = "hello world  Longest Substring";

    vector<string> vStr;


    //while (cin >> strIn)

    {

        string strTmp;

        for (int i = 0; i < strIn.size(); i++)

        {

            if (' ' == strIn[i])

            {

                if (!strTmp.empty())

                {

                    vStr.push_back(strTmp);

                    strTmp.clear();

                }


                continue;

            }


            strTmp += strIn[i];

        }


        /* 最后一个单词 */

        vStr.push_back(strTmp);


        #if 0

        for (int i = 0; i < vStr.size(); i++)

        {

            cout << vStr[i] << endl;

        }

        #endif

        vector<string>::iterator itVStr = vStr.end() - 1;

        cout << *itVStr << endl;

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容