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

Output:

Number of vowels: 10
Number of consonants: 17

C

Output:

Number of vowels: 10
Number of consonants: 17

JAVA

Output:

Number of vowels: 10
Number of consonants: 17

C#

Output:

Number of vowels: 10
Number of consonants: 17

PHP

Output:

Number of vowels: 10
Number of consonants: 17

Next TopicPrograms List




Latest Courses