- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
- Related Articles
- Count spaces, uppercase and lowercase in a sentence using C
- Java program to find the percentage of uppercase, lowercase, digits and special characters in a String
- C Program for LowerCase to UpperCase and vice-versa
- Replacing upperCase and LowerCase in a string - JavaScript
- Maximum distinct lowercase alphabets between two uppercase in C++
- Convert string to lowercase or uppercase in Arduino
- Java program to convert a string to lowercase and uppercase.
- Conversion of whole String to uppercase or lowercase using STL in C++
- Check if lowercase and uppercase characters are in same order in Python
- C++ program to convert kth character to lowercase
- Check input character is alphabet, digit or special character in C
- Making a Java String All Uppercase or All Lowercase.
- Converting Odd and Even-indexed characters in a string to uppercase/lowercase in JavaScript?
- How to convert lowercase letters in string to uppercase in Python?
- C# Program to convert first character uppercase in a sentence
