Vowels:- a,e,i,o,u,A,E,I,O,U
In this question, we have to count the number of consonants and vowels.
How to Approach :-
For this, we must traverse the whole string while traversing take a character and check whether it is a consonant or vowel. If it is a consonant increase the count of consonants by 1 otherwise increase the count of vowels by 1.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class NumbersOfVowelsAndConsonants { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int vowels = 0, consonants =0; String lowerCase = str.toLowerCase(); for (int i = 0; i <str.length(); i++) { char c = lowerCase.charAt(i); if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') vowels++; else if (c > 'a' && c <= 'z') consonants++; } System.out.println("vowels = "+vowels); System.out.println("consonants = "+consonants); } } |
Python
input_str = input("Enter a string: ") vowels, consonants = 0, 0 for char in input_str: if char.lower() in ['a', 'e', 'i', 'o', 'u']: vowels += 1 elif 'a'<char.lower()<='z': consonants += 1 print("Vowels:",vowels) print("Consonants:",consonants) |