Reputation: 49
This is what I have so far, I'm new to this so any help would be appreciated.
public static String removeVowels(String input) {
String s = "";
int f = 0;
for(int i = 0; i < input.length(); i++){
if(c == 'a'|c == 'e'|c == 'i'|c == 'o'|c =='u' | c == 'A' | c == 'E' | c == 'I' | c == 'O' | c == 'U')
f = 1;
else{
s = s + i;
f = 0;
}
}
return s;
}
Upvotes: 0
Views: 11371
Reputation: 1
public class RemoveVowels {
public static void main (String [] args) {
String str = "Hello Good Morning";
String s1 = str.replaceAll("[AEIOUaeiou]" , "");
System.out.println(s1);
}
}
Upvotes: 0
Reputation: 803
With the for loop requirement:
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();
}
You could potentially pretty this up in a number of ways, but the above should work.
Without the for loop requirement:
public static String removeVowels(String input) {
return input.replaceAll("[aAeEiIoOuU]","");
}
Upvotes: 4