Explore Courses Blog Tutorials Interview Questions
0 votes
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>();











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

        char c = s.charAt(i);

        if (!vowels.contains(c)) {




    return sb.toString();



public static void main(String[] args) {




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