Write a Java program ( Java 8 version ) to Replace Vowel letter with Capital the given String . Example – "Engineer".
The output is – EngInEEr.
Write a Java program ( Java 8 version ) to Replace Vowel letter with Capital the given String . Example – "Engineer".
I could not able to find the solution.
>Solution :
You can use the String#chars method to stream the characters, and utilize the IntStream#mapToObject method to map the values to String values.
You’ll have to cast letter to a char each time though, since the chars method uses an IntStream.
String string = "Engineer";
string = string.chars().mapToObj(letter -> {
switch ((char) letter) {
case 'a', 'e', 'i', 'o', 'u' -> {
return String.valueOf((char) letter).toUpperCase();
}
default -> {
return String.valueOf((char) letter);
}
}
}).collect(Collectors.joining());
Output
EngInEEr