In this article, we will discuss the concept of the Cpp code: count vowel, consonant, digit, special character and space
In this post, we are going to learn how to count the total number of Vowels, consonants, digits, special character and Space of the given string in C++ programming language
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, special characters and Spaces 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];//declare and initialize char array int i; int digits=0,spaces=0,spe_Char=0;//display and initialize variables int vCount=0,cCount=0; cout<<"Please enter the string \n"; gets(str);//stored the string entered by user for(i=0; str[i] != '\0'; i++){ if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u' || //check vowels str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' ){ vCount++; } else if( (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ // check consonants cCount++; } else if(str[i]>='0' && str[i]<='9') // check digits { digits++; } else if(str[i]==' '){ // check spaces spaces++; } else{ spe_Char++; } } cout<<"\nTotal Vowels: "<<vCount; //display total number of vowels cout<<"\nTotal consonants: "<<cCount; //display total number of consonants cout<<"\nTotal digits: "<<digits; //display total number of digits cout<<"\nTotal white space : "<<spaces; //display total number of spaces cout<<"\nTotal special characters: "<<spe_Char; //display total number of special characters getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string code4coding.com @ 1234 you Total vowels: 7 Total consonants: 9 Total digits: 5 Total white space: 2 Total white special characters: 2
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, 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];//declare and initialize char array int i; int digits=0,spaces=0,spe_Char=0;//display and initialize variables int vCount=0,cCount=0; cout<<"Please enter the string \n"; gets(str);//stored the string entered by user i=0; while(str[i] != '\0'){ if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u' || str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' ){ vCount++; } else if( (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ cCount++; } else if(str[i]>='0' && str[i]<='9') { digits++; } else if(str[i]==' '){ spaces++; } else{ spe_Char++; } i++; } cout<<"\nTotal Vowels: "<<vCount; cout<<"\nTotal consonants: "<<cCount; cout<<"\nTotal digits: "<<digits; cout<<"\nTotal white space : "<<spaces; cout<<"\nTotal special characters: "<<spe_Char; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string programming_solution : code4coding.com Total vowels: 12 Total consonants: 20 Total digits: 1 Total white space: 2 Total white special characters: 3
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, digits, 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];//declare and initialize char array int i; int digits=0,spaces=0,spe_Char=0;//display and initialize variables int vCount=0,cCount=0; cout<<"Please enter the string \n"; gets(str);//stored the string entered by user i=0; do{ if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u' || str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' ){ vCount++; } else if( (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){ cCount++; } else if(str[i]>='0' && str[i]<='9') { digits++; } else if(str[i]==' '){ spaces++; } else{ spe_Char++; } i++; }while(str[i] != '\0'); cout<<"\nTotal Vowels: "<<vCount; cout<<"\nTotal consonants: "<<cCount; cout<<"\nTotal digits: "<<digits; cout<<"\nTotal white space : "<<spaces; cout<<"\nTotal special characters: "<<spe_Char; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string code_4_coding:code for coding Total vowels: 9 Total consonants: 14 Total digits: 1 Total white space: 2 Total white special characters: 3
Approaches
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 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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…