g 6.8 LAB: Acronyms An acronym is a word formed from the initial letters of words in a set phrase. Write a program whose input is a phrase and whose output is an acronym of the input. If a word begins with a lower case letter, don't include that letter in the acronym. Assume there will be at least one upper case letter in the input.

Answer :

ammary456

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.