words_maxmin_1.cpp

来自「A group of word-analysis examples for C+」· C++ 代码 · 共 44 行

CPP
44
字号
/*
*  Description:
*
*    Report the longest and the shortest words in your input.
*
*  History:
*
*    Initial version created by Royal, Mar. 2004.
*
*  Notes:
*
*    This code has been written to conform to standard C++ and STL. It has been
*    compiled successfully using GNU C++ 3.2, Borland C++ 5.5, and Visual C++ 7.0.
*/

#include <iostream>
#include <string>
#include <limits>

using namespace std;

int main()
{
    int maxsize(0), minsize(100);
    string maxword, minword, temp;
    cout << "please input words:";
    while(cin >> temp)
    {
        int size = temp.size();
        if (size > maxsize)
        {
            maxsize = size;
            maxword = temp;
        }
        if(size < minsize)
        {
            minsize = size;
            minword = temp;
        }
    }
    cout << "The longest word is \"" << maxword << "\"; it's length is " << maxsize << endl;
    cout << "The shortest word is \"" << minword << "\"; it's length is " << minsize << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?