Count Number of Vowels and Consonants in the String
“Count Number of Vowels and Consonants in the String ” is a basic problem of string data structure. Here, we are given a string of length ‘n’ and our task is to count the number of vowels and consonants present in the string.
Example:
Input: Help12me!@study#bro Output: The number of vowels in Help12me!@study#bro is 4 The number of consonants in Help12me!@study#bro is 10
The steps to program to Count Number of Vowels and Consonants in the String is as follows:
- Scan Input string.
- Initialize vowelCount = 0 and consonantCount = 0.
- Traverse the string in the loop and check whether given character if consonant or vowel.
- If vowel, increase vowelCount by 1. Else if character is consonant, increase consonantCount by 1.
C++ Program to Count Number of Vowels and Consonants in the String is as follows:
/* Program to Count Number of Vowels and Consonants in the String */
#include<bits/stdc++.h>
#define len 1000
using namespace std;
int main()
{
char str[len];
cout<<"Enter the string:\n";
cin>>str;
int vowelCount = 0;
int consonantCount = 0;
for(int i = 0; i < len && str[i] != ' /* Program to Count Number of Vowels and Consonants in the String */ #include<bits/stdc++.h> #define len 1000 using namespace std; int main() { char str[len]; cout<<"Enter the string:\n"; cin>>str; int vowelCount = 0; int consonantCount = 0; for(int i = 0; i < len && str[i] != '\0'; i++) { /* If character is vowel, increase vowel count*/ if(str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' || str[i] == 'I' || str[i] == 'i' || str[i] == 'O' || str[i] == 'o' || str[i] == 'U' || str[i] == 'u') vowelCount++; /* Else if character is alphabet, increase consonantCount */ else if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z')) consonantCount++; } cout<<"\nThe number of vowels in "<<str<<" is "<<vowelCount; cout<<"\nThe number of consonants in "<<str<<" is "<<consonantCount; }
'; i++)
{
/* If character is vowel, increase vowel count*/
if(str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' || str[i] == 'I' || str[i] == 'i' || str[i] == 'O' || str[i] == 'o' || str[i] == 'U' || str[i] == 'u')
vowelCount++;
/* Else if character is alphabet, increase consonantCount */
else if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))
consonantCount++;
}
cout<<"\nThe number of vowels in "<<str<<" is "<<vowelCount;
cout<<"\nThe number of consonants in "<<str<<" is "<<consonantCount;
}
OUTPUT: Enter the string: Help12me!@study#bro The number of vowels in Help12me!@study#bro is 4 The number of consonants in Help12me!@study#bro is 10 Time Complexity: O(n), where n is the length of the string.
Related Posts:
- Program to Check if String is alphabetic or numeric or alpha-numeric.
- Program to copy one String to Another.
- Program to Concatenate Two Strings.
- Program to Check whether given String is Palindrome or not.
- Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
- Program to count total number of words in a string.
- Program to find smallest and largest word of the String.
- Program to find Most Frequent Character of the String.
- Program to Remove all the blank spaces from the String.
- Program to check if String is isogram or not.
- Program to Reverse Each word of the String.
- Program to Print All the Substring of the Given String.
- Program to find longest palindromic Substring.
- Program to check Anagram Strings.
- Program to check whether two Strings are Rotation of each other or not.
- Program to check Palindromic Anagram.
- Program to print all the Palindromic Substring of the String.
- Program to check Panagram String.
- Program to find first non-repeating character of the String.
- Program to merge two sorted arrays.