Q. Write an algorithm and program to count the total number of vowels and consonants in a string.



Vowels :- Five of the 26 alphabet letters are vowels: A, E, I, O, and U.
Consonants :- Other than 5 Vowels all other characters are Consonants.
Example :-
Input: str1 = "letsfindcourse"
Output:
Vowels : 5
Consonants : 9

Algorithm to Count Vowels And Consonants

//Algorithm to count the total number of vowels and consonants in a string.
START
Step 1 ->  Take a string as str.
Step 2 ->  for i=0 to length of the string str
convert each letter to lowercase
if a character contain a, e, i, o, u :
increase the count of vowels by 1.
else
increase the count of consonants by 1.
Step 3 ->   Show the count of vowels and consonants.
STOP


Program To Count Vowels And Consonants

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Count The Total Number Of Vowels And Consonants In A String.
    #include <stdio.h>
    #include <string.h> 
    #include <ctype.h>
    int main() 
    { 
    int i, count_vowel = 0, count_consonant = 0;  
    char str[] = "This is a program to count the total number of vowels and consonants in a string.";  
    for(i = 0; i < strlen(str); i++){  
    str[i] = tolower(str[i]);  
    if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {  
    count_vowel++;  
    }  
    else if(str[i] >= 'a' && str[i] <= 'z'){  
    count_consonant++;  
    }  
    }  
    printf("Number of vowels in the given sentence is : %d\n", count_vowel);  
    printf("Number of consonant in the given sentence is : %d", count_consonant);  
    } 
    
    //C++ Program To Count The Total Number Of Vowels And Consonants In A String.
    #include <iostream>
    using namespace std; 
    int main()
    {
    int i, count_vowel = 0, count_consonant = 0;  
    char str[] = "This is a program to count the total number of vowels and consonants in a string.";  
    int len=sizeof(str) / sizeof(str[0]);
    for(i = 0; i < len; i++){  
    str[i] = tolower(str[i]);  
    if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {  
    count_vowel++;  
    }  
    else if(str[i] >= 'a' && str[i] <= 'z'){  
    count_consonant++;  
    }  
    }  
    cout<<"Number of vowels in the given sentence is :"<<count_vowel<<"\n";  
    cout<<"Number of consonant in the given sentence is :"<<count_consonant<<"\n";  
    return 0;
    }
    
    //Java Program To Count The Total Number Of Vowels And Consonants In A String.
    public class LFC
    {
    public static void main(String[] args) {
    int count_vowel = 0, count_consonant = 0;  
    String str = "This is a program to count the total number of vowels and consonants in a string.";  
    str = str.toLowerCase();  
    for(int i = 0; i < str.length(); i++) {  
    if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {  
    count_vowel++;  
    }  
    else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {    
    count_consonant++;  
    }  
    }  
    System.out.println("Number of vowels in the given sentence is : " + count_vowel);  
    System.out.println("Number of consonant in the given sentence is :" + count_consonant);  
    }
    }
    
    //Python Program To Count The Total Number Of Vowels And Consonants In A String.
    count_vowel = 0;  
    count_consonant = 0;  
    str = "This is a program to count the total number of vowels and consonants in a string.";  
    str = str.lower();  
    for i in range(0,len(str)):   
    if str[i] in ('a',"e","i","o","u"):  
    count_vowel = count_vowel + 1;  
    elif (str[i] >= 'a' and str[i] <= 'z'):  
    count_consonant = count_consonant + 1;  
    print("Number of vowels in the given sentence is :" );  
    print(count_vowel);  
    print("Number of consonant in the given sentence is :" ); 
    print(count_consonant);  
    
    //C# Program To Count The Total Number Of Vowels And Consonants In A String.
    using System;                      
    public class LFC  
    {  
    public static void Main()  
    {  
    int count_vowel = 0, count_consonant = 0;  
    string str = "This is a program to count the total number of vowels and consonants in a string.";  
    str = str.ToLower();  
    for(int i = 0; i < str.Length; i++) {  
    if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {  
    count_vowel++;  
    }  
    else if(str[i] >= 'a' && str[i]<='z') {  
    count_consonant++;  
    }  
    }  
    Console.WriteLine("Number of vowels in the given sentence is :" + count_vowel);  
    Console.WriteLine("Number of consonant in the given sentence is :" + count_consonant);  
    }  
    }  
    //PHP Program To Count The Total Number Of Vowels And Consonants In A String.
    <?php
    $count_vowel = 0;  
    $count_consonant = 0;  
    $str = "This is a program to count the total number of vowels and consonants in a string.";  
    $str = strtolower($str);  
    for($i = 0; $i < strlen($str); $i++) 
    {  
    if( $str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u') {  
    $count_vowel++;  
    }  
    else if($str[$i] >= 'a' && $str[$i] <= 'z') {  
    $count_consonant++;  
    }  
    }  
    echo "Number of vowels in the given sentence is :" , $count_vowel;  
    echo "\nNumber of consonants in the given sentence is :" , $count_consonant;  
    ?>
    //C Program To Count The Total Number Of Vowels And Consonants In A String.
    #include <stdio.h>
    #include <string.h> 
    #include <ctype.h>
    int main() 
    { 
    int i, count_vowel = 0, count_consonant = 0;  
    char str[] = "This is a program to count the total number of vowels and consonants in a string.";  
    for(i = 0; i < strlen(str); i++){  
    str[i] = tolower(str[i]);  
    if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {  
    count_vowel++;  
    }  
    else if(str[i] >= 'a' && str[i] <= 'z'){  
    count_consonant++;  
    }  
    }  
    printf("Number of vowels in the given sentence is : %d\n", count_vowel);  
    printf("Number of consonant in the given sentence is : %d", count_consonant);  
    } 
    

    Output

    Number of vowels in the given sentence is : 23
    Number of consonant in the given sentence is : 42
    




    Recommended Programs

       Program to make a simple calculator.
       Program to find all pairs of elements in an integer array whose sum is equal to a given number.