Display vowel, digits & blank spaces in string

Q. Write a Java program which accepts string from user and displays vowel characters, digits and blank space separately.

Answer:

In this example we separate the vowels, digits and spaces from the given string.

Alphabets.java

import java.io.*;
class Alphabets
{
     public static void main(String args[]) throws IOException
     {
          String str;
          int vowels = 0, digits = 0, blanks = 0;
          char ch;

          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          System.out.print("Enter a String : ");
          str = br.readLine();
          for(int i = 0; i < str.length(); i ++)
          {
               ch = str.charAt(i);
               if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
               ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
                    vowels ++;
               else if(Character.isDigit(ch))
                    digits ++;
               else if(Character.isWhitespace(ch))
                    blanks ++;
          }
          System.out.println("Vowels : " + vowels);
          System.out.println("Digits : " + digits);
          System.out.println("Blanks : " + blanks);
          System.out.println("\n\n\n ");
     }
}


Output:

alphabet check