An acronym is a word formed by taking the first letters of the words in a phrase
ID: 3676585 • Letter: A
Question
An acronym is a word formed by taking the first letters of the words in a phrase and making a word from them. For example, RAM is an acronym for "random access memory." Write a function acronym that takes one input string and then outputs the acronym for that phrase. Note: the acronym should be all uppercase, even if the words in the phrase are not capitalized. For example acronym ('Random access memory') should return 'RAM'. Note that since we have not covered lists, you may not use any string functions or methods that return lists (i.e.. split is not allowed).Explanation / Answer
import javax.swing.JOptionPane;
public class Acronym
{
public static void main(String[] args)
{
String phrase;
String Acronym="";
phrase = JOptionPane.showInputDialog(null, "Enter your phrase");
Acronym=Acronym+phrase.charAt(0);
for(int i=0;i<(phrase.length())-1;i++)
{
if(phrase.charAt(i)==' '&&phrase.charAt(i+1)!=' ')
Acronym=Acronym+phrase.charAt(i+1);
}
System.out.println(Acronym.toUpperCase().trim());
}
}
Example output:-
Enter your phrase Random access memory
RAM
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.