Back
I want to write a program that would remove vowels from a string using for loop. How can I do it?
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"));}}
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.
31k questions
32.8k answers
501 comments
693 users