In this article, we will discuss the concept of Program to count vowel and consonant of a string in C++ using ASCII value
In this post, we are going to learn how to count the vowels and consonants in the given string using ASCII value in C++ programming language
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using for loop in C++ language
Program 1
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100];//character array declaration int i; int vowelCount=0,consonantCount=0;//counter variable declaration cout<<"Please enter a string\n"; gets(str);//store the entered string for(i=0; str[i]!='\0'; i++){ if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117 ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){ vowelCount++;//vowel counter incremented by 1 } else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){ consonantCount++;//consonant counter incremented by 1 } } cout<<"The number of vowels: "<<vowelCount; cout<<"\nThe number of consonant: "<<consonantCount; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string vowel count The number of vowels:: 4 The number of consonants: 6
Approach
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using while loop in C++ language
Program 1
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100]; int i; int vowelCount=0,consonantCount=0; cout<<"Please enter a string\n"; gets(str); i=0; while(str[i]!='\0'){ if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117 ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){ vowelCount++; } else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){ consonantCount++; } i++; } cout<<"The number of vowels: "<<vowelCount; cout<<"\nThe number of consonant: "<<consonantCount; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string
consonant count
The number of vowels:: 5
The number of consonants: 9
Approach
The program allows the user to enter a string thereafter It counts the vowels and consonants of the given string using do-while loop in C++ language
Program 1
#include <iostream> #include <conio.h> #include <stdio.h> using namespace std; int main() { char str[100];//char array declaration int i; int vowelCount=0,consonantCount=0; //counter variable declaration cout<<"Please enter a string\n"; gets(str);//store the entered string from user i=0; do{ if(str[i] == 97|| str[i] == 101|| str[i] == 105|| str[i] == 111|| str[i] == 117 ||str[i] == 65|| str[i] == 69|| str[i] == 73|| str[i] == 79|| str[i] == 85 ){ vowelCount++;//vowel counter incremented by 1 } else if((str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90 )){ consonantCount++;//consonant counter incremented by 1 } i++; }while(str[i]!='\0'); cout<<"The number of vowels: "<<vowelCount; cout<<"\nThe number of consonant: "<<consonantCount; getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string vowels and consonants The number of vowels:: 5 The number of consonants: 9
Approach
Suggested for you
Data type in C++ language
If-else statements in C++ Language
Similar post
C code to check whether the Alphabet is vowel or consonant
Python code to check whether the Alphabet is vowel or consonant
Java code to check whether the Alphabet is vowel or consonant
C++ code to check whether the Alphabet is vowel or consonant
Java program to count vowels and consonants in a string
C program to count vowels and consonants in a string
Python program to count vowels and consonants in a string
Program to count vowel and consonant of a string in Java using ASCII value
Program to count vowel and consonant of a string inC++ using ASCII value
Program to count vowel and consonant of a string in C using ASCII value
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…