Posts

Showing posts with the label consonants

How to count the number of vowels and consonants in a String

In this blog post, I will be demonstrating a Java program that counts the number of vowels and consonants in a String. Consider the following code sample: public class CountVowelsAndConsonantsDemo { private static List<Character> vowels = Arrays.asList('a','e','i','o','u','A','E','I','O','U'); public static void main(String[] args) { System.out.println("Enter a String:"); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); char[] inputArr = input.toCharArray(); int vowelCount = 0; int consonantCount = 0; for(char c:inputArr){ if(vowels.contains(c)){ vowelCount++; } else consonantCount++; } System.out.println("Number of vowels:"+vowelCount); System.out.println("Number of consonants:"+consonantCount); }}   The code declares and initalizes a list ‘vowels’ with all the vowels in lowercas...