zoukankan      html  css  js  c++  java
  • 最长单词 (分割字符串)(蓝桥杯-算法提高)

    编写一个函数,输入一行字符,将此字符串中最长的单词输出。 
    输入仅一行,多个单词,每个单词间用一个空格隔开。单词仅由小写字母组成。所有单词的长度和不超过100000。如有多个最长单词,输出最先出现的。

    Input

    Output

    Sample Input

    copy
    I am a student

    Sample Output

    copy
    student


    int main()
    {
        string my_string;
        while(getline(cin, my_string, '
    '))
        {
            char *pch,*p;
            char str[100000+5];
            strcpy(str, my_string.c_str());
            pch = strtok(str, " ");
            k=0;
            while(pch != NULL)
            {
                if(strlen(pch)>k)
                {
                    p=pch;
                    k=strlen(pch);
                }
                //cout << pch << endl;
                pch = strtok(NULL, " ");  // 注意这里是NULL
            }
            cout<<p<<endl;
        }
        ok;
    }
    所遇皆星河
  • 相关阅读:
    2020 11 21
    2020 11 20
    2020 11 19
    2020 11 18
    2020 11 17
    2020 11 16
    2020 11 15
    2020 11 14
    2020 11 14
    第五周学习进度报告
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11539527.html
Copyright © 2011-2022 走看看