Java program to remove vowels from a string

Remove vowel from String

In this problem, we’re going to code a java program to remove vowel from a string. Take a string input from the user and store it in a variable called as “s” (in this case) .‘A’, ‘E’, ‘I’, ‘O’, ‘U’ are five vowels out of 26 characters in English alphabet letters.
Here we will use replaceAll() method to replace vowel from string the main work of replaceAll() method in java is to replace something by passing two arguments to this method i.e one is an old string and second is new string that you’re replacing the old string with, so here we’re passing a regular expression to replace vowels with blank spaces.

 

 

java progrma to remove vowel from string

Algorithm

  • Take a String input from user and store is in a variable called as s 
  • After that take String variable s1 with empty String
  • After that call replaceAll() on s object 
  • Write regex on replaceAll() method like this s1 = s.replaceAll(“[aeiou]”, “”);
  • Print s variable

Code in Java

import java.util.Scanner;

public class RemoveVowelsfromAString {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in); 
		System.out.print("Enter a String : ");
        String s = sc.nextLine();
        String s1 = "";
        s1 = s.replaceAll("[aeiou]", ""); 
        System.out.println("String after removing vowel : "+s1); 
	}

}

This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)

Output

Enter a String : prepinsta
String after removing vowel : prpnst

One comment on “Java program to remove vowels from a string”