C++ code to Count Alphabets, Numeric, Special character and Space in a string
C++ code to Count Alphabets, Numeric, Special character and Space
In this article, we will discuss the concept of the C++ code to Count Alphabets, Numeric, Special character and Space
In this post, we are going to learn how to count the total number of Alphabets, Numeric, Special character and Space of the given string in C++ programming language
C++ code to Count Alphabets, Numeric, Special character and Space in a string
Code to Count Alphabets, Numeric, Special character and Space using for loop
The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space 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 Alphabets=0,num=0,special=0,space=0; cout<<"Please enter the string \n"; gets(str); for(i=0; str[i] != '\0'; i++){ //check for Aphabets if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) { //check Alphabets Alphabets++; }else if(str[i]>='0' && str[i]<='9') { //check numbers num++; } else if(str[i]==' '){//ceck space space++; } else{ special++; } } cout<<"\nAlphabet letters: "<<Alphabets; cout<<"\nnumbers: "<<num; cout<<"\nSpace: "<<space; cout<<"\nSpecial characters: "<<special; getch(); return 0; }
When the above code is executed, it produces te following result
Pleases enter the string Code 4 you:code4coding.com Alphabet letters: 20 Numbers: 2 Space: 2 Special characters: 2
Approach
- Declare a Character array as char str[100];
- Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- A for loop is used to count every total of the given string.
- It is initialized as i=0, checks the condition 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’) || (str[i]>=’a’ && str[i]<=’z’)), if it is true, The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +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 num becomes num+1(num=num+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 space becomes space+1(space=space+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 Alphabets, Numeric, Special character and Space of the given string
Code to Count Alphabets, Numeric, Special character and Space using while loop
The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space of the given string using While loop in C++ programing language
Program 2
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i; int Alphabets=0,num=0,special=0,space=0; cout<<"Please enter the string \n"; gets(str); i=0; while(str[i] != '\0'){ //check for Alphabets if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) { Alphabets++; }else if(str[i]>='0' && str[i]<='9') { //check numeric num++; } else if(str[i]==' '){ //check space space++; } else{ special++; //find special } i++; } cout<<"\nAlphabets letters: "<<Alphabets; cout<<"\nnumbers: "<<num; cout<<"\nSpace: "<<space; cout<<"\nSpecial characters: "<<special; getch(); return 0; }
When the above code is executed, it produces te following result
Pleases enter the string Basic program:code4coding.com Alphabet letters: 25 Numbers: 1 Space: 1 Special characters: 2
Approach
- Declare a Character array as char str[100];
- Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- A while loop is used to count every total of the given string.
- It is initialized as i=0, checks the condition 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’) || (str[i]>=’a’ && str[i]<=’z’)), if it is true, The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +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 num becomes num+1(num=num+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 space becomes space+1(space=space+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 Alphabets, Numeric, Special character and Space of the given string
Code to Count Alphabets, Numeric, Special character and Space using do-while loop
The program allows the user to enter a String and then it counts and display the total number of Alphabets, Numeric, Special character and Space 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 Alphabets=0,num=0,special=0,space=0; cout<<"Please enter the string \n"; gets(str); i=0; do{ //check Alphabets if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) { Alphabets++; }else if(str[i]>='0' && str[i]<='9') { //Check numeric num++; } else if(str[i]==' '){ //check space space++; } else{ special++; //find special } i++; } while(str[i] != '\0'); cout<<"\nAlphabets letters: "<<Alphabets; cout<<"\nnumbers: "<<num; cout<<"\nSpace: "<<space; cout<<"\nSpecial characters: "<<special; getch(); return 0; }
When the above code is executed, it produces te following result
Pleases enter the string Basic program:code4coding.com Alphabet letters: 26 Numbers: 2 Space: 2 Special characters: 2
Approach
- Declare a Character array as char str[100];
- Declare and initialize integer variables as Alphabets=0,num=0,space=0,Special=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- A do-while loop is used to count every total of the given string.
- It is initialized as i=0, checks the condition 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’) || (str[i]>=’a’ && str[i]<=’z’)), if it is true, The Alphabets becomes Alphabets + 1( Alphabets = Alphabets +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 num becomes num+1(num=num+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 space becomes space+1(space=space+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 Alphabets, Numeric, Special character and Space of the given string
Suggested for you
Similar post
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character
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