Java Program to Print Vowels and Consonants in String

Java Program to Print Vowels and Consonants in String | The alphabets ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (in uppercase) and ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels and remaining alphabets are called consonants.

To develop a Java program to print vowels and consonants in a given String, we have to consider the following points.

1) The given String can be in uppercase or lowercase or both, so either we have to write separate logic for both cases or convert the given string to uppercase or lowercase and write logic only for one case.

In the String class, toUpperCase() method is given to convert the given string to uppercase, and toLowerCase() method is given to convert string to lowercase.

// convert string to uppercase
String str = "[email protected]";
str = str.toUpperCase();

The toUpperCase() method converts “[email protected]” to “[email protected]”. Therefore, we have to write logic to check only for ‘A’, ‘E’, ‘I’, ‘O’, ‘U’

2) To print vowels and consonants we have to check each character of the given String. The charAt(int i) method of the String class can be used to iterate through each character of the String.

2) Only characters belong to ‘a’ to ‘z’ and ‘A’ to ‘Z’ can be vowel or consonants, other characters like @, #, 0-9 e.t.c. are not alphabets. A String can contain alphabets, digits, and special characters. For this, we can use the ASCII value of the characters in Java.

Check character is an alphabet?

// for uppercase character
if(ch >= 'A' && ascii <= 'Z')
then it is an alphabet

// for lowercase character,
if(ch >= 'a' && ascii <= 'z')
then it is an alphabet

3) The given String can contain 0 to 5 vowels and 0 to 21 consonants. We can’t predict the exact number of vowels and constants in the given String. Therefore, it is a better idea to take the help of Set collection instead of an array. The array has a fixed length but collections are dynamic growable.

4) While printing vowels and constants from the given String we should print it only once. Example:- The String “Java” contains the vowel ‘a’, the character ‘a’ should be print only for one time, not two times. The HashSet Collection stores only unique values.

5) The add() method is used to add the elements into the Collection.


Print Vowels & Consonants From Java String

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class PrintVowelConsonant {

   public static void main(String[] args) {
      // declare variables
      Scanner scan = null;
      String str = null;
      Set<Character> vowels = null;
      Set<Character> consonants = null; 
      char ch = '\0';

      // create Scanner class object
      scan = new Scanner(System.in);

      // take input
      System.out.print("Enter String :: ");
      str = scan.nextLine();

      // convert String to upperCase
      str = str.toUpperCase();

      // HashSet collection
      vowels = new HashSet<Character>();
      consonants = new HashSet<Character>();

      // check each character
      for(int i=0; i<str.length(); i++) {
         ch = str.charAt(i);

         // check it is alphabet or not
         if(ch >= 'A' && ch <= 'Z') {

            // check vowel or consonant
            if( checkVowel(ch) ) {
               vowels.add(ch);
            } else {
               consonants.add(ch);
            }
         }
      }

      // display results
      System.out.println("Vowels :: " + vowels);
      System.out.println("Consonants :: " + consonants);

      // close Scanner object
      scan.close();
		
   }

   private static boolean checkVowel(char ch) {
      if(ch == 'A' || ch == 'E' || ch == 'I'
            || ch == 'O' || ch == 'U')
      return true;
      return false; // else return false;
   }

}

Output for different test-cases:-

Enter String :: [email protected]
Vowels :: [A, O]
Consonants :: [P, R, W, G, K, M, N]

Enter String :: Hello, How are you?
Vowels :: [A, E, U, O]
Consonants :: [R, W, H, Y, L]

Using this program we can also count the number of unique vowels or consonants. Since characters are stored in the Set collection so we can use the size() method to get the number of elements in each collection.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Similar Java Program Examples

Leave a Comment

Your email address will not be published. Required fields are marked *