如何循环计数,数字和字母的程序?

时间:2015-07-27 02:53:38

标签: c++ loops for-loop while-loop

该程序假设计算输入的数字,字(即Abc,fish ...)大写字母和最长字长。 我无法正确编写代码来识别字数和输入的字母数。当我运行它时,只看到的数字似乎有效,我不知道如何使程序计数输入最长的单词。我在正确的轨道上?非常感谢任何帮助!

我的代码:

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

    int main(){ 
        char s = '0';
        char sentinel = '.';
        int countWords = 0;
        int countDigits = 0;
        int countLetters = 0;
        int countUppers = 0;
        cout << "Enter your string: " << endl;
        while (cin >> s){
            if (isdigit(s)){
                countDigits++;
            }
            else if (isupper(s)){
                countUppers++;
                }
            else if (isspace(s)){
                countWords++;
            }

            cout << "# of Digits: " << countDigits << endl;
            cout << "# of Uppers: " << countUppers << endl;
            cout << "# of Words: " << countWords << endl;
            cout << "Longest word length" << countLetters << endl;
        }

        system("PAUSE");
    }
    /*getline(cin, s);
    const int size = s.length();
    cout << "The total number of characters entered is: " << size << endl;*/'

1 个答案:

答案 0 :(得分:1)

在这里,这是一个如何解决问题的示例。希望你觉得它很有用。

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>

bool IsNumber(const std::string _str)
{
    return !_str.empty() && std::find_if(_str.begin(),_str.end(), [](char c) { return !std::isdigit(c); }) == _str.end();
}

bool IsUpper(const std::string _str)
{
    return !_str.empty() && std::find_if(std::begin(_str), std::end(_str), [](char c) { return !(std::isupper(c)); }) == _str.end();
}

int main()
{
    std::vector<std::string> listInputs;
    std::string input;
    const int numberOfInputs = 5;

    std::cout << "Input 5 different stuff. Words, Uppercase words, digits..." << std::endl << std::endl;

    for (size_t i = 0; i < numberOfInputs; ++i)
    {
        //ask the question
        std::cout << "Input " << i+1 << ": " << std::flush;
        std::cin >> input;
        std::cout << std::endl;
        listInputs.push_back(input);
    }

    //check some shit
    int countWords = 0;
    int countDigits = 0;
    int countLetters = 0;
    int countUppers = 0;
    int longestWordSize = 0;

    for (const auto& i : listInputs)
    {
        //check if its a digit
        if (IsNumber(i))
        {
            ++countDigits;
        }
        else
        {
            longestWordSize = (i.length() > longestWordSize) ? i.length() : longestWordSize;
            ++countWords;
            countLetters += i.length();
            if (IsUpper(i))
            {
                ++countUppers;
            }
        }
    }

    std::cout << std::endl << "######## THE RESULT BABY ########" << std::endl << std::endl;
    std::cout << "Number of words found: " << countWords << std::endl;
    std::cout << "Number of Digits found: " << countDigits << std::endl;
    std::cout << "Number of uppercase words found: " << countUppers << std::endl;
    std::cout << "The longest word length was: " << longestWordSize << std::endl;

    std::cin.get();
    return 0;
}
相关问题