Computer scienceProgramming languagesJavaWorking with dataCollectionsCollection implementationsThe Collection hierarchy implementations

ArrayDeque

Remove vowels

Report a typo

You are given a string of characters. Write a function in Java that uses an ArrayDeque to remove all the vowels from the string.

Input: The function takes a single argument—a string s.

Output: The function should return the string after removing all the vowels (a, e, i, o, u, A, E, I, O, U).

Sample Input 1:

Hello, World!

Sample Output 1:

Hll, Wrld!
Write a program in Java 17
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
import java.util.Set;

public class Main {
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u');

public static String removeVowels(String s) {
Deque<Character> deque = new ArrayDeque<>();

for (char ch : s.toCharArray()) {
if (!isVowel(ch)) {
...
}
}

StringBuilder result = new StringBuilder(deque.size());
while (!deque.isEmpty()) {
...
}

return result.toString();
}

private static boolean isVowel(char ch) {
return VOWELS.contains(Character.toLowerCase(ch));
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputStr = scanner.nextLine();
System.out.println(removeVowels(inputStr));
}
}

Create a free account to access the full topic