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 lowercase and uppercase. It then iterates through the characters in the input String. It checks if each character is present in the vowels list, if so it increments the vowels counter, otherwise it increments the consonants counter.

When you run this code, it will print the following output:

Enter a String:HELLONumber of vowels:2Number of consonants:3

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set