Mindtree|Remove all vowel from user input a string in java

 Remove all vowel from user input a string in java

In this java program, we have to delete all occurrences of vowel characters from a given string and then print it on screen


The logic behind this question is simple. It is to remove the vowels in the given string and display the input string without vowels (i.e with only consonants). 
Here we are using two approach.


1.Using Indexof()  Method

Java String indexOf() method is used to find the index of a specified character or substring in a given String.

 Method returns -1 if the specified char/substring is not found in the particular String.




import java.util.Scanner;
public class R_Vowels {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.err.println("Enter the String :");
        String s1=sc.nextLine();
        String s2="AEIOUaeiou";
        String s3=" ";
     for(int i=0;i<s1.length();i++)
     {
    char c=s1.charAt(i);
    int o=s2.indexOf(c);
     System.err.println(o);
    if(o==-1)
    {
          s3=s3+c; 
    }
     
     }
     System.err.println(s3);     
}
}



2. ReplaceAll method


public class D_Vowels 
{
         public static void main(String args[])
 {
        String s1, output;
        Scanner scanner = new Scanner(System.in);
 
        System.out.println("Enter a String");
        s1 = scanner.nextLine();
 
        // remove Vowel from input String using replaceAll method
        output = s1.replaceAll("[AEIOUaeiou]", "");
 
        System.out.println("Output String\n" + output);
    }
}



Comments

Post a Comment

Please do not write any spam link in the comment box.

Popular posts from this blog

RCM|Consequences of imbalanced Nutrition|WHO share daily need

Java Program for shop Bill to Calculate total amount of product with discount

Start your day with healthy PVMF breakfast|Nutricharge prodiet