codescracker


java

Java Program to Remove Vowels from String



« Previous Program Next Program »


Remove Vowels from String

To delete or remove vowels from string in Java programming, you have to first ask to the user to enter the string and start deleting/removing all the vowels present in the string as shown in the following two programs.

Here, we have two methods to delete vowels from the string, first method is shortcut method that uses the method replaceAll() to replace all the vowels with no-space (in short deleting all the vowels using this method shortly) from the string then copy that string into another string without having any vowels whereas the second method is to remove/delete vowels manually.

Java Programming Code to Delete Vowels from String

Here first, we uses the shortcut method to remove/delete all the vowels from the string, and second, we will use manual method to remove all the vowels from the string.

Delete Vowels from String - using Function

Here we uses the function named replaceAll() to remove/delete all the vowels from the string. Let's look at the following program :

/* Java Program Example - Remove/Delete all Vowels from String */
		
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
       String strOrig, strNew;
       Scanner scan = new Scanner(System.in);
       
       System.out.print("Enter a String : ");
       strOrig = scan.nextLine();
       
       System.out.print("Removing Vowels from The String [" +strOrig+ "]\n");
       strNew = strOrig.replaceAll("[aeiouAEIOU]", "");
	   
       System.out.print("All Vowels Removed Successfully..!!\nNow the String is :\n");
              
       System.out.print(strNew);
   }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program delete vowels from string

Delete Vowels from String - Manual Method

Following Java program removes all the vowels present in the string manually. Let's look at the following program :

/* Java Program Example - Remove/Delete Vowels from String */
		
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
       String str, r;
       Scanner scan = new Scanner(System.in);
       
       System.out.print("Enter a String : ");
       str = scan.nextLine();

       System.out.print("Removing Vowels from String [" +str+ "]\n");
       r = removeVowels(str);
	   
       System.out.print("Vowels Removed from the Entered String Successfully..!!\nNow the String is :\n");
       System.out.print(r);
   }

   private static String removeVowels(String s)
   {
     String finalString = "";
     int i;

     for(i=0; i<s.length(); i++)
     {
       if (!isVowel(Character.toLowerCase(s.charAt(i))))
       {
         finalString = finalString + s.charAt(i);
       }
     }
     return finalString;
   }

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

When the above Java Program is compile and executed, it will produce the following output:

Java Program remove vowels from string

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test