Wednesday, June 23, 2021
Home coding Write a program to count number of vowel and consonant in string

Write a program to count number of vowel and consonant in string

Given a string we have to count the number of vowel and consonant present in a string.

so what is vowel and consonant?

vowel consist of 5 letter a,e,i,o,u. and other 21 letter of english alphabet are consonant. so we just need to find that only.

example:

Input:asdfe
output:vowel:2 consonant :3

input:aooasd
output:vowel:4 consonant:2

Logic:

Step1:find the count  of vowel in a string 
step2: then to find consonant subtract the length of string with vowel count;
step 3:consonant=string length-vowel count;
# include <bits/stdc++.h> 
using namespace std; 
int isvowel(string s)
{
    int count=0;
    for(int i=0;i<s.length();i++)
    {
    if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
    {
       count++; 
    }
    }
    return count;
}
int main() 
{ 
cout<<"Enter string"<<endl;
string s;
cin>>s;
int length=s.length();
int vowel_count=isvowel(s);
int consonant=length-vowel_count;
cout<<"vowel: "<<vowel_count<<" "<<"consonant: "<<consonant<<endl;


} 
enter string:AADASFS
vowel: 3 consonant: 4

Time Complexity:O(N)

space Complexity:O(N)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments