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

风格不统一 提交于 2019-12-01 14:54:50

Try:

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

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 ;
    }
}
#include<stdio.h>
main() 
{
int upper = 0, lower = 0,digit=0,special=0;
char ch[80];
int i;

printf("\nEnter The String : ");
gets(ch);

for(i = 0; ch[i]!='\0';i++)

{
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
else if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
else if(ch[i] >='0' && ch[i] <='9')
digit++;
else if(ch[i]!=' ')
special++;
}


printf("\nUppercase Letters : %d", upper);
printf("\nLowercase Letters : %d", lower);
printf("\nDigits : %d", digit);
printf("\nSpecial Characters : %d",special);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!