C++ code to Count Uppercase, Lowercase,Special character and Numeric values
C++ code to Count Uppercase, Lowercase, Special character and Numeric values
In this article, we will discuss the concept of the C++ code to Count Uppercase, Lowercase, Special character and Numeric values in C++ language
In this post, we are going to learn how to count the total number of Uppercase, Lowercase, Special character and Numeric values of the given String in C++ programming language
Count Uppercase, Lowercase, Special character and Numeric values using for loop
The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using for loop in C++ programing language
Program 1
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i; int upper=0,lower=0,num=0,special=0;; cout<<"Please enter the string \n"; gets(str); for(i=0; str[i] != '\0'; i++){ //check for uppercase if(str[i]>='A' && str[i]<='Z') { upper++; }else if(str[i]>='a' && str[i]<='z') {//check lower case lower++; }else if(str[i]>='1' && str[i]<='9') {//check numeric value num++; } else{ special++; } } cout<<"Upper case letters: "<<upper; cout<<"\nLower case letters: "<<lower; cout<<"\nnumbers: "<<num; cout<<"\nSpecial characters: "<<special; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string My_Code:code4coding.com Upper case letters: 2 Lower case letters: 17 Numbers: 1 Special characters: 3
Approach
- Declare a String variable as character array char str[100];
- Declare and initialize an integer variables as int i, upper=0,lower=0,num=0,Special=0;
- The user asked to enter a string to count upper case, lower case, Numeric, Special characters
- The given string is stored in the variable str;
- A for-loop is used to count total characters of the given string
- It is initialized as i=0, checks the test-expression whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str[i]>=’A’ && str[i]<=’Z’), if it is true, The upper becomes upper + 1(upper=upper+1);
- When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The lower becomes lower + 1(lower=lower+1);
- When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
- When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
- Finally, the program displays the total number of the upper case, lower case, Numeric, Special characters of the given string
C++ code to Count Uppercase, Lowercase, Special character and Numeric values using while loop
The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using while loop in C++ programing language
Program 1
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i; int upper=0,lower=0,num=0,special=0;; cout<<"Please enter the string \n"; gets(str); i=0; while(str[i] != '\0'){ //check for uppercase if(str[i]>='A' && str[i]<='Z') { upper++; //check for lower case }else if(str[i]>='a' && str[i]<='z') { lower++; //check for numbers }else if(str[i]>='1' && str[i]<='9') { num++; } else{ special++; } i++; } cout<<"Upper case letters: "<<upper; cout<<"\nLower case letters: "<<lower; cout<<"\nnumbers: "<<num; cout<<"\nSpecial characters: "<<special; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string code4coding.com:2020 Upper case letters: 0 Lower case letters: 13 Numbers: 4 Special characters: 4
Approach
- Declare a String variable as character array char str[100];
- Declare and initialize an integer variables as int i, upper=0,lower=0,num=0,Special=0;
- The user asked to enter a string to count upper case, lower case, Numeric, Special characters
- The given string is stored in the variable str;
- A while-loop is used to count total characters of the given string using the count variable
- It is initialized as i=0, checks the test-expression whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str[i]>=’A’ && str[i]<=’Z’), if it is true, The upper becomes upper + 1(upper=upper+1);
- When it is false, control moves to the else-if part and evaluates the test-expression of else-if. If it is true The lower becomes lower + 1(lower=lower+1);
- When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. If it is true The num becomes num+1(num=num+1);
- When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
- Finally, the program displays the total number of the upper case, lower case, Numeric, Special characters of the given string
C++ code to Count Uppercase, Lowercase, Special character and Numeric values using do-while loop
The program allows the user to enter a String and then it counts and display the total number of upper case, lower case, numeric values and special characters of the given string using do-while loop in C++ programing language
Program 3
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i; int upper=0,lower=0,num=0,special=0;; cout<<"Please enter the string \n"; gets(str); i=0; do{ //check for uppercase if(str[i]>='A' && str[i]<='Z') { upper++; //check for lower case }else if(str[i]>='a' && str[i]<='z') { lower++; //check for numbers }else if(str[i]>='1' && str[i]<='9') { num++; } else{ special++; } i++; } while(str[i] != '\0'); cout<<"Upper case letters: "<<upper; cout<<"\nLower case letters: "<<lower; cout<<"\nnumbers: "<<num; cout<<"\nSpecial characters: "<<special; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string code4coding.com:CodingSolution Upper case letters: 2 Lower case letters: 25 Numbers: 1 Special characters: 2
Approach
- Declare a String variable as character array char str[100];
- Declare and initialize an integer variables as int i, upper=0,lower=0,num=0,Special=0;
- The user asked to enter a string to count upper case, lower case, Numeric, Special characters
- The given string is stored in the variable str;
- A do-while-loop is used to count total characters of the given string.
- It is initialized as i=0, checks the test-expression whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str[i]>=’A’ && str[i]<=’Z’), if it is true, The upper becomes upper + 1(upper=upper+1);
- When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The lower becomes lower + 1(lower=lower+1);
- When it is false, control moves to the next else-if part and evaluates the test-expression of else-if. if it is true The num becomes num+1(num=num+1);
- When it is false, control moves to the else part and executes the else part statements. the special becomes special+1
- Finally, the program displays the total number of the upper case, lower case, Numeric, Special characters of the given string
Suggested for you
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of characters in the given string including space
C program to count the total number of characters in the given string including space
Python program to count the total number of characters in the given string including space