How to code a C++ program which counts the number of uppercase letters, lowercase letters and integers in an inputted string?

前端 未结 2 888
你的背包
你的背包 2021-01-17 08:58

I\'m looking for a simple and basic way (ideal for beginners to learn the easiest way) to write a program in C++ which gets a string from the user and outputs the number of

2条回答
  •  忘掉有多难
    2021-01-17 09:40

    Try:

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        cout << " Enter text: ";
        string s;
        if(getline(cin, s))
        {
            size_t count_lower = count_if(s.begin(), s.end(), 
                   [](unsigned char ch) { return islower(ch); });
            cout << "lowers: " << count_lower ;
    
            size_t count_upper = count_if(s.begin(), s.end(),    
                   [](unsigned char ch) { return isupper(ch); });
            cout << "uppers: " << count_upper ;
    
            size_t count_digit = count_if(s.begin(), s.end(),    
                   [](unsigned char ch) { return isdigit(ch); });
            cout << "digits: " << count_digit ;
        }
    }
    

提交回复
热议问题