Adsense 728 * 15

Search

Custom Search

Program to Count Number of Vowels and Consonants in a String

Write a C, C++ program to count number of vowels and consonants in a string. Given an input string, We have to write a code to count number of vowels and Consonants in a string.

Program to check whether a input character is vowel or not

Programming Interview Questions

C, C++ Program to Count Number of Vowels and Consonants in a String
C, C++ Program to Count Number of Vowels and Consonants in a String



Before solving this problem let's understand what is vowel and consonant.

What is Vowel and Consonants ?

In english alphabet, A, E, I, O, U is called a vowel and remaining alphabets which is not a vowel is known as consonant.

How to read string with spaces using scanf

Programming Questions on Strings

C Program to Count Number of Vowels and Consonants in a String


#include<stdio.h>
#include<string.h>


int main(){
   

    char str[200];
    
    int i,vowel=0,consonant=0;
   

    printf("Enter any string");
    fgets(str,200,stdin);
    

    /* Length of a string. */
    
    int len = strlen(str);
    
     /* Traverse a string */
     for(i = 0; i < len; i++){
          
         /* Check for vowel and increment the count. */

         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' ) {
             
                vowel++;
                
             /* Check for consonant. */

            } else if((str[i]>='a' && str[i]<='z') 
                       || (str[i]>='A' && str[i]<='Z')) {
             
               consonant++;
            }
     }
 
    printf("The number of vowels and consonants in a 
            string is %d %d",vowel ,consonant);

    return 0;
}




C++ Program to Count Number of Vowels and Consonants in a String


#include <iostream>
#include <string.h>

using namespace std;

int main() {
 
     char str[300];
     int  vowel = 0, consonant = 0;
 
     cout << "Enter a string \n";
     cin.getline(str, sizeof(str));

      /* Length of a string. */
    
      int len = strlen(str);
    
     /* Traverse a string */
     for(int i = 0; i < len; i++){
          
       /* Check for vowel and increment the count. */

       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' ) {
             
                vowel++;
                
             /* Check for consonant. */

       } else if((str[i] >= 'a' && str[i] <= 'z') 
                  || (str[i] >= 'A' && str[i] <= 'Z')) {
             
               consonant++;
       }
     }
     
     cout << "The number of vowels and consonants 
                    in a string is "
                   << vowel << "  " << consonant;
 
    return 0;
}



Output -

Enter a string  - I love cprogrammingcode.com

The number of vowels and consonants in a string is  9   15

1 comment:

  1. #include
    #include
    using namespace std;
    int main(){
    char line[150];
    int i,v,c,ch,d,s,o;
    o=v=c=ch=d=s=0;
    cout << "Enter a line of string: " << endl;
    cin.getline(line, 150);
    for(i=0;line[i]!='\0';++i)
    {
    if(line[i]=='a'
    || line[i]=='e'
    || line[i]=='i'
    || line[i]=='o'
    || line[i]=='u'
    || line[i]=='A'
    || line[i]=='E'
    || line[i]=='I'
    || line[i]=='O'
    || line[i]=='U')
    ++v;
    else if((line[i]>='a'&& line[i]<='z')
    || (line[i]>='A'&& line[i]<='Z'))
    ++c;
    else if(line[i]>='0'&&c<='9')
    ++d;
    else if (line[i]==' ')
    ++s;
    }
    cout << " Vowels: " << v << endl;
    cout << " Consonants: " << c << endl;
    cout << " Digits: " << d << endl;
    cout << " White Spaces: " << d << endl;
    return 0;
    }
    Output

    Enter a line of string:
    programming is fun 1 234
    Vowels: 5
    Consonants: 11
    Digits: 4
    White Spaces: 4

    ReplyDelete