Count Vowels and Consonants in Java

In this Java tutorial, we will learn the different ways of counting the number of vowels and consonants in a given string. We will explore the Java 8 Streams, simple iteration among other methods.

Note that in the English alphabet, there are 5 vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) and the rest are consonants. In contrast, in Java, we can have several kinds of characters such as alphabets, numbers, special characters, white spaces, etc. So our solution must exclude the non-alphabet characters exclusively.

1. Using Stream to Count Vowels and Consonants

To count the number of vowels and consonants in a String, we can iterate over the characters of the String, and then use Collectors.groupingBy() into two groups. The first group will contain the vowels and the second group will contain the consonants.

We use the Collectors.groupingBy collector to partition the elements of the stream into two groups based on whether the character is a vowel or a consonant. The key of the resulting map will be either "vowels" or "consonants".

Finally, we use the Collectors.counting() downstream collector to count the occurrences of each group.

String input = "howtodoinjava.com".toLowerCase();

Map<String, Long> counts = input.chars() 
    .mapToObj(c -> (char) c) 
    // Filter characters to include only vowels and consonants
    .filter(c -> "aeiou".indexOf(c) != -1 || "bcdfghjklmnpqrstvwxyz".indexOf(c) != -1) 
    // Collect the filtered characters into a map
    .collect(Collectors.groupingBy( 
        c -> "aeiou".indexOf(c) != -1 ? "vowels" : "consonants", 
        Collectors.counting() 
    ));

System.out.println("Counts: " + counts); 

The program output:

Counts: {vowels=7, consonants=9}

2. Using Iteration to Count Vowels and Consonants

Follow the given algorithm to count the vowels and consonants separately.

  • Read an input string
  • Convert the string to lowercase so that comparisons can be reduced
  • Iterate over it’s characters one by one
  • If the current character matches with vowels (a, e, i, o, u ) then increment the vCount by 1
  • Else if any character lies between ‘a’ and ‘z’, then increment the count for cCount by 1
  • Print both the counts

Java example of using iteration to count vowels and consonants in a specified String.

String str = "howtodoinjava.com".toLowerCase();

int vCount = 0, cCount = 0;

for (int i = 0; i < str.length(); i++) {
	if ("aeiou".indexOf(str.charAt(i)) != -1) {
		vCount++;
	}
	else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {
		cCount++;
	}
}

System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);

Program output.

Number of vowels: 7
Number of consonants: 9

Drop me your questions related to Java programs to count the number of vowels and consonants in a given string.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode