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
- Define a string.
- Convert the string to lower case so that comparisons can be reduced. Else we need to compare with capital (A, E, I, O, U).
- If any character in string matches with vowels (a, e, i, o, u ) then increment the vcount by 1.
- If any character lies between 'a' and 'z' except vowels, then increment the count for ccount by 1.
- Print both the counts.
Complexity
O(n)
Input:
str = "This is a really simple sentence"
Output:
vowels = a, e, i, o, u
consonants = b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, z
Python
Output:
C
Output:
JAVA
Output:
C#
Output:
PHP
Output: