77.8k views
3 votes
The function below takes a single string parameter: sentence. Complete the function to return a list of strings indicating which vowels (a, e, i, o, and u) are present in the provided sentence. The case of the vowels in the original sentence doesn't matter, but the should be lowercase when returned as part of the list. The order of the strings in the list doesn't matter. For example, if the provided sentence was 'All good people.', your code could return ['a', 'e', 'o']. It could also return ['e', 'o', 'a']. One way to implement this would be to use the filter pattern starting with the list of all vowels.

User Trimack
by
3.6k points

1 Answer

1 vote

Answer:

import java.util.*;

public class num3 {

public static void main(String[] args) {

String sentence = "All good people";

System.out.println("The vowels in the sentence are ");

returnVowels(sentence);

}

public static void returnVowels(String word){

String newWord = word.toLowerCase();

String vowels ="";

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

if(newWord.charAt(i)=='a'||newWord.charAt(i)=='e'||newWord.charAt(i)==i||

newWord.charAt(i)=='o'||newWord.charAt(i)=='u'){

vowels = vowels+newWord.charAt(i);

//System.out.print(newWord.charAt(i)+" ");

}

}

String [] chars = vowels.split("");

Set<String> uniqueVowels = new LinkedHashSet<>();

for (String s : chars) {

uniqueVowels.add(s);

}

System.out.println(uniqueVowels);

}

}

Step-by-step explanation:

In Java

Create the method/function to receive a string as argument

Change the string to all small letters using the toLowerMethod

Iterate through the string using if statement identify the vowels and store in a new String

Since the sentence does not contain unique vowel characters, use the set collections in java to extract the unique vowels and print out

User Siggemannen
by
3.7k points