Java program to remove vowels from a string using switch case code example

Example 1: Java 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);
   }
}

Example 2: Java program to delete vowels in a given string

// Java program to delete vowels in a given string
import java.util.*;
public class RemoveVowelsInString
{
   public static void main(String[] args)
   {
      String str = "Deekshit Prasad";
      System.out.println("Given string: " + str);
      str = str.replaceAll("[AaEeIiOoUu]", "");
      System.out.println("After deleting vowels in given a string: " + str);
   }
}

Example 3: Remove vowels from a string in java using for loop

// Remove vowels from a string in java using for loop
import java.util.ArrayList;
import java.util.List;
public class RemoveVowelsUsingForLoop
{
   public static void main(String[] args)
   {
      String str = "Hello world core java";
      String strResult = removeVowels(str);
      System.out.println("Remove vowels using for loop: " + strResult);
   }
   private static String removeVowels(String str)
   {
      List<Character> al = new ArrayList<Character>();
      al.add('a');
      al.add('e');
      al.add('i');
      al.add('o');
      al.add('u');
      al.add('A');
      al.add('E');
      al.add('I');
      al.add('O');
      al.add('U');
      StringBuffer sb = new StringBuffer(str.toLowerCase());
      for(int a = 0; a < sb.length(); a++)
      {
         if(al.contains(sb.charAt(a)))
         {
            sb.replace(a, a + 1, "");
            a--;
         }
      }
      return sb.toString();
   }
}