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

前端 未结 2 850
你的背包
你的背包 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:43

    #include
    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);
    }
    

提交回复
热议问题