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.
Example:
Input: str = "Java is A programming language!";
Output: Number of vowels: 11
Number of consonants: 15
Approach
Java
public class VowelConCount {public static void main(String[] args) {// Counter variable to store the count of vowels and consonantint vCount = 0, cCount = 0;// Declare a stringString str = "Java is A programming language!";// Converting entire string to lower case to reduce the comparisonsstr = str.toLowerCase();for (int i = 0; i < str.length(); i++) {// Checks whether a character is a vowelif (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'|| str.charAt(i) == 'u') {vCount++;}// Checks whether a character is a consonantelse if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {cCount++;}}System.out.println("Number of vowels: " + vCount);System.out.println("Number of consonants: " + cCount);}}
No comments:
Post a Comment