Java Program to remove vowels from an input string

Remove Vowels from a String 

In this article we will learn how to code a Java program to remove vowels from a string. ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ are five vowels out of 26 characters in English alphabet letters. Java programming is case sensitive, and hence lowercase and uppercase characters are considered differently, so we will have to check for both the cases and then we can remove the vowels from a string

Remove the vowels from a String in java

Approach

  • Step 1: Take input str as a String.
  • Step 2: Convert the string into character array.
  • Step 3: Traverse the complete array and check if the character present at particular index is vowel or not.
  • Step 4: If the character if vowel the continue the loop else concatenate the character into resultant string.
  • Step 5: Print the resultant string.

Code in Java

import java.util.*;
class Solution
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
char arr[]=str.toCharArray();
int n=arr.length;
String res="";
for(int i=0;i<n;i++)
{
if(arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u' || arr[i]=='A' || arr[i]=='E' || arr[i]=='I' || arr[i]=='O' || arr[i]=='U')
continue;
else
res=res+arr[i];
}
System.out.println(res);
}
}

Output :

Prepinsta
Prpnst

2 comments on “Java Program to remove vowels from an input string”


  • Kunal

    import java.util.*;
    class Main
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    String ss=””;
    for(int i=0;i<s.length();i++)
    {
    char c=s.charAt(i);
    if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u' && c!='A' && c!='E' && c!='I' && c!='O' && c!='U')
    ss=ss+c;
    }
    System.out.print(ss);
    }
    }


  • Kunal

    import java.util.*;
    class Main
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    String s=sc.next();
    int l=s.length();
    char c;
    String ss=””;
    for(int i=0;i<l;i++)
    {
    c=s.charAt(i);
    if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u' && c!='A' && c!='E' && c!='I' && c!='O' && c!='U')
    {
    ss=ss+c;
    }
    }
    System.out.print(ss);
    }
    }