Java program to remove vowels from a string using switch case

Let’s learn java program to remove vowels from a string using switch case.

Java program to remove vowels from a string using switch case

Write a java program to remove the vowels from the string is the most common java interview question.

java program to remove vowels from a string using switch case

Did you know to remove vowels using switch case. For example,

Before: Welcome to Flower Brackets blog!!
After: Wlcm t Flwr Brckts blg!!

Also read – classes and objects in java

Here’s the program to remove vowels from a string using switch case.

// remove vowels from string
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RemoveVowelsUsingSwitchCase
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String strFirst, str = "";
      char ch, chCase;
      int a, len;
      System.out.println("Please enter a sentence : ");
      strFirst = br.readLine();
      len = strFirst.length();
      for(a = 0; a < len; a++) 
      {
         ch = strFirst.charAt(a);
         chCase = Character.toLowerCase(ch);
         switch(chCase) 
         {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
               break;
            default:
               str = str + ch;
         }
      }
      System.out.println("String without vowels : " + str);
   }
}


Output:

Please enter a sentence : flower brackets
String without vowels : flwr brckts


Java program to remove consonants from a string

Here’s the java program to remove consonants from a string. For example,

Input string: hello world core java

Output string: eo o oe aa

Input string: flower brackets

Output string: oe ae

import java.util.Arrays;
import java.util.List;
public class RemoveConsonantsFromString
{
   public static void main(String[] args) 
   {
      String str = "hello world core java";
      System.out.println("Remove consonants from a string: ");
      System.out.println(removeConsonantsFunction(str));
   }
   static boolean checkAlphabet(char ch) 
   { 
      if(ch >= 'a' && ch <= 'z')
         return true;
      if(ch >= 'A' && ch <= 'Z') 
         return true; 
      return false; 
   }
   static String removeConsonantsFunction(String strConsonant)
   {
      Character[] chVowel = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
      List<Character> li = Arrays.asList(chVowel);
      StringBuffer sb = new StringBuffer(strConsonant);
      for(int a = 0; a < sb.length(); a++)
      {
         if(checkAlphabet(sb.charAt(a)) && !li.contains(sb.charAt(a))) 
         { 
            sb.replace(a, a + 1, ""); 
            a--; 
         }
      }
      return sb.toString(); 
   }
}


Output:

Remove consonants from a string: eo o oe aa


Also read – remove vowels from string java