Count Uppercase, Lowercase, special character and numeric values in C++


We are given a string containing Uppercase letters, Lowercase letters, specials characters and number values also. The task is to calculate the frequency of all types of characters, special characters and numeric values in the string.

Uppercase Letters − A - Z having ASCII values from 65 - 90 where, 65 and 90 are inclusive.

Lowercase Letter − a - z having ASCII values from 97 - 122 where, 97 and 122 are inclusive.

Numeric values − 0 - 9 having ASCII values from 48 - 57 where, 48 and 57 are inclusive.

Special Characters − !, @, #, $, %, ^, &, *

Input − str = Tutori@lPo!n&90

Output − Total Upper case letters in a string are − 2

Total lower case letters in a string are − 8

Total number in a string are − 2

total of special characters in a string are − 3

Input − str = WELc0m$

Output − Total Upper case letters in a string are − 3

Total lower case letters in a string are − 2

Total number in a string are − 1

total of special characters in a string are − 1

Approach used in the below program is as follows

  • Input the string containing uppercase letters, lowercase letters, special characters and numeric values.

  • Calculate the length of the string

  • Take variables to store the count of uppercase letters, lowercase letter, special characters and numeric values and intialises them with 0.

  • Start loop FOR from 0 till the size of a string

  • Inside the loop, check IF str[i] >= A and str[i] <= Z then increment the count of uppercase letters.

  • Inside the loop, check IF str[i] >= a and str[i] <= z then increment the count of lowercase letters.

  • Inside the loop, check IF str[i] >= 0 and str[i] <= 9 then increment the count of numeric values.

  • Else, increment the count of special characters.

  • Print the result

Example

 Live Demo

#include<iostream>
using namespace std;
//Count Uppercase, Lowercase, special character and numeric values
void count(string str){
   int Uppercase = 0;
   int Lowercase = 0;
   int digit = 0;
   int special_character = 0;
   for (int i = 0; i < str.length(); i++){
      if (str[i] >= 'A' && str[i] <= 'Z'){
         Uppercase++;
      }
      else if(str[i] >= 'a' && str[i] <= 'z'){
         Lowercase++;
      }
      else if(str[i]>= '0' && str[i]<= '9'){
         digit++;
      }
      else{
         special_character++;
      }
   }
   cout<<"Total Upper case letters in a string are: "<<Uppercase<< endl;
   cout<<"Total lower case letters in a string are: "<<Lowercase<< endl;
   cout<<"Total number in a string are: "<<digit<< endl;
   cout<<"total of special characters in a string are: "<<special_character<< endl;
}
int main(){
   string str = "Tutori@lPo!n&90";
   count(str);
   return 0;
}

Output

If we run the above code it will generate the following output −

Total Upper case letters in a string are: 2
Total lower case letters in a string are: 8
Total number in a string are: 2
total of special characters in a string are: 3

Updated on: 31-Aug-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements