Java program to remove vowels from a string

Hello everyone!! Welcome to flower brackets blog. Let us learn java program to remove vowels from a string.

java program to remove vowels from a string

Here in the below java program user enters a string as input. Then replaceAll() method replaces each substring of this string that matches given regular expression with the given replacement.

import java.util.Scanner;

public class RemoveVowelsInString
{
   public static void main(String[] args)
   {
      String actual;
      String present;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a string : ");
      actual = sc.nextLine();
      System.out.println("Before removing vowels : " + actual);
      present = actual.replaceAll("[aeiouAEIOU]", " ");
      System.out.println("After removing vowels : " + present);
      sc.close();
   }
}

Output :

Please enter a string : Deekshit Prasad
Before removing vowels : Deekshit Prasad
After removing vowels : Dksht Prsd


Similarly we can remove all vowels from string using a method,

import java.util.Scanner;

public class VowelsInJava 
{
   static String removeVowel(String strVowel)
   {
      String str = "";
      for(int a = 0; a < strVowel.length(); a++)
      {
         if(!isVowel(Character.toLowerCase(strVowel.charAt(a))))
         {
            str = str + strVowel.charAt(a);
         }
      }
      return str;
   }

   private static boolean isVowel(char ch)
   {
      String vowel = "aeiou";
      for(int a = 0; a < 5; a++)
      {
         if(ch == vowel.charAt(a))
         {
            return true;
         }
      }
      return false;
   }

   public static void main(String[] args) 
   {
      String strInput, strOutput;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter a string : ");
      strInput = sc.nextLine();
      strOutput = removeVowel(strInput);
      System.out.print("After removing vowels : " + strOutput);
      sc.close();
   }
}

Output :

Please enter a string : Deekshit Prasad
After removing vowels : Dksht Prsd

Related Posts