Skip to main content

Posts

Showing posts with the label consonants

Count the number of Consonants and Vowels in a string

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' ) ...