Java Program to Delete all Vowel Characters from String

Here is a Java program to remove all vowel alphabets from a string using replaceAll() method and without using any library function. In this java program, we have to delete all occurrences of vowel characters from a given string and then print it on screen.

For Example,
Input String : TechCrashCourse
Output String : TchCrshCrs

Here, we are going to use two approaches of removing vowel alphabets from strings.


Java program to delete vowels from string using replaceAll method.

Here we are going to use replaceAll() method of String class. This method replaces any occurrence of any vowel character(lowercase or uppercase) with "". We are using "[AEIOUaeiou]" regular expression, which matches with any vowel alphabet.

package com.tcc.java.programs;

import java.util.Scanner;

public class DeleteVowels {
    public static void main(String args[]) {
        String str, output;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a String");
        str = scanner.nextLine();

        output = str.replaceAll("[AEIOUaeiou]",
           "");

        System.out.println("Output String\n" 
           + output);
    }
}
Output
Enter a String
TechCrashCourse
Output String
TchCrshCrs
Enter a String
Apple
Output String
ppl

Java program to remove vowel characters from string without using library function.

package com.tcc.java.programs;

import java.util.Scanner;

public class DeleteVowelsLoop {
    public static void main(String args[]) {
        String str, output = "";
        int length, i;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a String");
        str = scanner.nextLine();
        length = str.length();

        // Deleting vowel alphabets from input string
        for (i = 0; i < length; i++) {
            if ("AEIOUaeiou".indexOf(str.charAt(i)) == -1) {
                output = output + str.charAt(i);
            }
        }

        System.out.println("Output String : " + output);
    }
}
Output
Enter a String
Apple
Output String : ppl
Enter a String
TechCrashCourse
Output String : TchCrshCrs

Recommended Posts
Java Program to Delete All Spaces from a String
Java Program to Delete a Word from Sentence
Java Program to Count Words in a Sentence
C++ Program to Remove all Non Alphabet Characters from a String
Java Program to Find Count of Each Character in a String
Java Program to Reverse a String
Java Program to Concatenate Two Strings
Java Program to Find Length of a String
Java Program to Copy a String
Java Program to Find Transpose of a Matrix
Java Program for Matrix Multiplication
All Java Programs