Write a java program for remove vowels from String

Write code to remove vowels from the even position in the string.

 

The return type of the output is string after removing all the vowels.

 

Input and Output Format:

Input is a string.

The output is a string.

Assume the first character is at position 1 in the given string.

Sample Input 1:

commitment

Sample Output 1:

cmmitmnt

Sample Input 2:

capacity

Sample Output 2:

Cpcty

Removing vowels from String in Java.

import java.util.Scanner;

public class Main {
                public static void main(String[] args) {
                			Scanner sc = new Scanner(System.in);
                			String s1 = sc.nextLine();
                                
                                System.out.println(removeEvenElements(s1));
                }
                public static String removeEvenElements(String s1) {
                                StringBuffer sb1=new StringBuffer();
                                for(int i=0;i<s1.length();i++)
                                                if((i%2)==0)
                                                                sb1.append(s1.charAt(i));
                                                else if((i%2)!=0)
                                                                if(s1.charAt(i)!='a' && s1.charAt(i)!='e' && s1.charAt(i)!='i' && s1.charAt(i)!='o' && s1.charAt(i)!='u')
                                                                                if(s1.charAt(i)!='A' && s1.charAt(i)!='E' && s1.charAt(i)!='I' && s1.charAt(i)!='O' && s1.charAt(i)!='U')
                                                                                 sb1.append(s1.charAt(i));
                                return sb1.toString();
                }
}

Add a Comment

Your email address will not be published. Required fields are marked *

two × 3 =