Count Uppercase, Lowercase, special character and numeric values - GeeksforGeeks

11 Mar, 2022
Given a string, write a program to count the occurrence of Lowercase characters, Uppercase characters, Special characters, and Numeric values. Examples:  
Input : #GeeKs01fOr@gEEks07
Output : 
Upper case letters : 5
Lower case letters : 8
Numbers : 4
Special Characters : 2

Input : *GeEkS4GeEkS*
Output :
Upper case letters : 6
Lower case letters : 4
Numbers : 1
Special Characters : 2
 
Approach :  
Scan string str from 0 to length-1.check one character at a time on the basis of ASCII values if(str[i] >= 65 and str[i] <=90), then it is uppercase letter,  if(str[i] >= 97 and str[i] <=122), then it is lowercase letter,  if(str[i] >= 48 and str[i] <=57), then it is number,  else it is a special characterPrint all the counters.
Scan string str from 0 to length-1.
check one character at a time on the basis of ASCII values if(str[i] >= 65 and str[i] <=90), then it is uppercase letter,  if(str[i] >= 97 and str[i] <=122), then it is lowercase letter,  if(str[i] >= 48 and str[i] <=57), then it is number,  else it is a special character
if(str[i] >= 65 and str[i] <=90), then it is uppercase letter,  
if(str[i] >= 97 and str[i] <=122), then it is lowercase letter,  
if(str[i] >= 48 and str[i] <=57), then it is number,  
else it is a special character
Print all the counters.
 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count the uppercase,// lowercase, special characters// and numeric values#include<iostream>using namespace std; // Function to count uppercase, lowercase,// special characters and numbersvoid Count(string str){    int upper = 0, lower = 0, number = 0, special = 0;    for (int i = 0; i < str.length(); i++)    {        if (str[i] >= 'A' && str[i] <= 'Z')            upper++;        else if (str[i] >= 'a' && str[i] <= 'z')            lower++;        else if (str[i]>= '0' && str[i]<= '9')            number++;        else            special++;    }    cout << "Upper case letters: " << upper << endl;    cout << "Lower case letters : " << lower << endl;    cout << "Number : " << number << endl;    cout << "Special characters : " << special << endl;} // Driver functionint main(){    string str = "#GeeKs01fOr@gEEks07";    Count(str);    return 0;}
// Java program to count the uppercase,// lowercase, special characters// and numeric valuesimport java.io.*; class Count{    public static void main(String args[])    {        String str = "#GeeKs01fOr@gEEks07";        int upper = 0, lower = 0, number = 0, special = 0;         for(int i = 0; i < str.length(); i++)        {            char ch = str.charAt(i);            if (ch >= 'A' && ch <= 'Z')                upper++;            else if (ch >= 'a' && ch <= 'z')                lower++;            else if (ch >= '0' && ch <= '9')                number++;            else                special++;        }         System.out.println("Lower case letters : " + lower);        System.out.println("Upper case letters : " + upper);        System.out.println("Number : " + number);        System.out.println("Special characters : " + special);    }}
# Python 3 program to count the uppercase,# lowercase, special characters# and numeric values # Function to count uppercase, lowercase,# special characters and numbersdef Count(str):    upper, lower, number, special = 0, 0, 0, 0    for i in range(len(str)):        if str[i].isupper():            upper += 1        else if str[i].islower():            lower += 1        else if str[i].isdigit():            number += 1        else:            special += 1    print('Upper case letters:', upper)    print('Lower case letters:', lower)    print('Number:', number)    print('Special characters:', special) # Driver Codestr = "#GeeKs01fOr@gEEks07"Count(str) # This code is contributed# by Sushma Reddy
// C# program to count the uppercase,// lowercase, special characters// and numeric valuesusing System; class Count {         public static void Main()    {                 String str = "#GeeKs01fOr@gEEks07";        int upper = 0, lower = 0;        int number = 0, special = 0;         for(int i = 0; i < str.Length; i++)        {            char ch = str[i];            if (ch >= 'A' && ch <= 'Z')                upper++;            else if (ch >= 'a' && ch <= 'z')                lower++;            else if (ch >= '0' && ch <= '9')                number++;            else                special++;        }        Console.WriteLine("Upper case letters : " + upper);        Console.WriteLine("Lower case letters : " + lower);        Console.WriteLine("Number : " + number);        Console.Write("Special characters : " + special);    }} // This code is contributed by Nitin Mittal.
<?php// PHP program to count the uppercase,// lowercase, special characters// and numeric values // Function to count uppercase, lowercase,// special characters and numbersfunction Countt($str){    $upper = 0;    $lower = 0;    $number = 0;    $special = 0;    for ($i = 0; $i < strlen($str); $i++)    {        if ($str[$i] >= 'A' &&            $str[$i] <= 'Z')            $upper++;        else if ($str[$i] >= 'a' &&                 $str[$i] <= 'z')            $lower++;        else if ($str[$i]>= '0' &&                 $str[$i]<= '9')            $number++;        else            $special++;    }    echo "Upper case letters: " , $upper,"\n" ;    echo "Lower case letters : " ,$lower,"\n" ;    echo "Number : " , $number ,"\n";    echo "Special characters : ", $special ;}     // Driver Code    $str = "#GeeKs01fOr@gEEks07";    Countt($str); // This code is contributed by nitin mittal.?>
<script>      // JavaScript program to count the uppercase,      // lowercase, special characters      // and numeric values       // Function to count uppercase, lowercase,      // special characters and numbers      function Count(str)      {        var upper = 0,          lower = 0,          number = 0,          special = 0;        for (var i = 0; i < str.length; i++)        {          if (str[i] >= "A" && str[i] <= "Z") upper++;          else if (str[i] >= "a" && str[i] <= "z") lower++;          else if (str[i] >= "0" && str[i] <= "9") number++;          else special++;        }        document.write("Upper case letters: " + upper + "<br>");        document.write("Lower case letters : " + lower + "<br>");        document.write("Number : " + number + "<br>");        document.write("Special characters : " + special + "<br>");      }       // Driver function      var str = "#GeeKs01fOr@gEEks07";      Count(str);             // This code is contributed by rdtank.    </script>
Output:  
Upper case letters: 5
Lower case letters : 8
Number : 4
Special characters : 2
Time Complexity: O(N), where N is the string length
Auxiliary Space: O(1)This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
SamyuktaSHegde
Code_r
sushmareddys2312
rdtank
rohitsingh07052
simmytarika5
MakeMyTrip
School Programming
Strings
MakeMyTrip
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C++ Classes and Objects
Constructors in C++
Interfaces in Java
Operator Overloading in C++
Copy Constructor in C++
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types
Python program to check if a string is palindrome or not