Answer :
Answer:
import java.util.Scanner;
public class Acronym {
public static String buildAcronym(String phrase) {
String[] separatedWords = phrase.split(" ");
String acronymAlphabets = "";
for(int i = 0; i < separatedWords.length; ++i) {
if(Character.isUpperCase(separatedWords [i].charAt(0))) {
acronymAlphabets += Character.toUpperCase(separatedWords [i].charAt(0));
}
}
return acronymAlphabets;
}
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.println(buildAcronym(userInput.nextLine()));
}
}
Explanation:
- Create a buildAcronym method inside the Acronym class that takes a phrase entered by user as an argument from the main method.
- Break down the phrase into separate words by using the built-in split function.
- Loop through the separated word and check if the current selected character is a capital alphabet and then add that alphabet to acronymAlphabets variable and return it.
- In the main method, get the phrase as an input from user.
- Finally call the buildAcronym method and display the result.