Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I want to write a program that would remove vowels from a string using for loop. How can I do it?

1 Answer

0 votes
by (13.1k points)

You can do something like this:

import java.util.*;

public class Main

{

private static String removeVowels(String s) {

    if (s == null) {

        return null;

    }

    StringBuilder sb = new StringBuilder();

    Set<Character> vowels = new HashSet<Character>();

    vowels.add('a');

    vowels.add('A');

    vowels.add('e');

    vowels.add('E');

    vowels.add('i');

    vowels.add('I');

    vowels.add('o');

    vowels.add('O');

    vowels.add('u');

    vowels.add('U');

    for (int i = 0; i < s.length(); i++) {

        char c = s.charAt(i);

        if (!vowels.contains(c)) {

            sb.append(c);

        }

    }

    return sb.toString();

}

    

public static void main(String[] args) {

System.out.println(removeVowels("HI,HELLO"));

}

}

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)

Browse Categories

...