TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Count the Total Number of Vowels and Consonants in a String

Program to Count the Total Number of Vowels and Consonants in a String on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to count the total number of vowels and consonants in a string.

In this program, our task is to count the total number of vowels and consonants present in the given string. As we know that, The characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known as the consonant. To solve this problem, First of all, we need to convert every upper-case character in the string to lower-case so that the comparisons can be done with the lower-case vowels only not upper-case vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match each character with all the vowels, i.e., a, e, I, o, u. If the match is found, increase the value of count by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given below.

Algorithm

  1. Define a string.
  2. Convert the string to lower case so that comparisons can be reduced. Else we need to compare with capital (A, E, I, O, U).
  3. If any character in string matches with vowels (a, e, i, o, u ) then increment the vcount by 1.
  4. If any character lies between 'a' and 'z' except vowels, then increment the count for ccount by 1.
  5. Print both the counts.

Complexity

O(n)

Solution

Python

vcount = 0;
ccount = 0;
str = "This is a really simple sentence";
 
#Converting entire string to lower case to reduce the comparisons
str = str.lower();
for i in range(0,len(str)): 
    #Checks whether a character is a vowel
    if str[i] in ('a',"e","i","o","u"):
        vcount = vcount + 1;
    elif (str[i] >= 'a' and str[i] <= 'z'):
        ccount = ccount + 1;
print("Total number of vowel and consonant are" );
print(vcount);
print(ccount);

Output:

Number of vowels: 10
Number of consonants: 17

C

#include <stdio.h>
int main()
{
    //Counter variable to store the count of vowels and consonant
   int i, vCount = 0, cCount = 0;
   char str[] = "This is a really simple sentence";
   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') {
           //Increments the vowel counter
            vCount++;
        }
        else if(str[i] >= 'a' && str[i] <= 'z'){
            //Increments the consonant counter
            cCount++;
        }
   }
   printf("Number of vowels : %d\n", vCount);
   printf("Number of consonant : %d", cCount);
   return 0;
}

Output:

Number of vowels: 10
Number of consonants: 17

JAVA

public class CountVowelConsonant {
    public static void main(String[] args) {
        
        //Counter variable to store the count of vowels and consonant
        int vCount = 0, cCount = 0;
        
        //Declare a string
        String str = "This is a really simple sentence";
        
        //Converting entire string to lower case to reduce the comparisons
        str = str.toLowerCase();
        
        for(int i = 0; i < str.length(); i++) {
            //Checks whether a character is a vowel
            if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
                //Increments the vowel counter
                vCount++;
            }
            //Checks whether a character is a consonant
            else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {  
                //Increments the consonant counter
                cCount++;
            }
        }
        System.out.println("Number of vowels: " + vCount);
        System.out.println("Number of consonants: " + cCount);
    }
}

Output:

Number of vowels: 10
Number of consonants: 17

C#

using System;                    
public class Program
{
    public static void Main()
    {
        //Counter variable to store the count of vowels and consonant
        int vCount = 0, cCount = 0;
        
        //Declare a string
        string str = "This is a really simple sentence";
        
        //Converting entire string to lower case to reduce the comparisons
        str = str.ToLower();
        
        for(int i = 0; i < str.Length; i++) {
            
            //Checks whether a character is a vowel
            if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
                
                //Increments the vowel counter
                vCount++;
            }
            
            //Checks whether a character is a consonant
            else if(str[i] >= 'a' && str[i]<='z') {
                
                //Increments the consonant counter
                cCount++;
            }
        }
        Console.WriteLine("Number of vowels : " + vCount);
        Console.WriteLine("Number of consonant : " + cCount);
    }
}

Output:

Number of vowels: 10
Number of consonants: 17

PHP

<!DOCTYPE html>
<html>
<body>
<?php
    //Counter variable to store the count of vowels and consonant
    $vCount = 0;
    $cCount = 0;
    
    //Declare a string
    $str = "This is a really simple sentence";
        
    //Converting entire string to lower case to reduce the comparisons
    $str = strtolower($str);
    for($i = 0; $i < strlen($str); $i++) {
            
    //Checks whether a character is a vowel
    if( $str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u') {
        //Increments the vowel counter
        $vCount++;
    }
            
    //Checks whether a character is a consonant
    else if($str[$i] >= 'a' && $str[$i] <= 'z') {
    
        //Increments the consonant counter
        $cCount++;
        }
    }
    echo "Number of vowels : " , $vCount;
    echo "<br>";
    echo "\nNumber of consonants : " , $cCount;
?>
</body>
</html>

Output:

Number of vowels: 10
Number of consonants: 17

Next TopicPrograms List




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf