In: Computer Science
Working in a technical field can be confusing because of all the acronyms. For example, CPU means Central Processing Unit, HDMI means High-Definition Multimedia Interface, and SMART means Self-Monitoring Analysis and Reporting Technology. Wouldn't it be nice if there was a way to translate all these acronyms automatically?
Create a new file named Translate.java that contains the following method:
public static String expandAll(String[] acronyms, String[] definitions, String text)
This method should replace every instance of an acronym in the text with its corresponding definition. For example, expandAll({"JMU", "CS"}, {"James Madison University", "Computer Science"}, "JMU CS rocks!") would return the string "James Madison University Computer Science rocks!".
You may assume that the two arrays will be the same length. You may also assume that the text will be reasonable English with correct grammar. In particular, there will be only one space between each word. Each sentence will either end with a space or a newline character. Punctuation not at the end of the sentence will always be followed by whitespace (i.e., you should be able to handle "Hi, Mom" but you won't have to handle "Hi,Mom").
Be careful not to replace acronyms in the middle of a word. For example, the string "CS uses MACS!" should expand to "Computer Science uses MACS!", not "Computer Science uses MAComputer Science!". You might find it helpful to use a Scanner to process the string one word at a time.


Program Code to Copy:
public class Translate {
   public static String expandAll(String[] acronyms,
String[] definitions, String text) {
      
   String result = ""; // the final processed text to
return
       String word = ""; // a single word
in text
       char lastChar = ' '; // last
character of a word in text
       boolean isPunctuation;
       boolean acronymFound; // true only
if a word in text is found in acronyms
     
       String[] wordsInText = text.split("
"); // get all words in text
      
       for(int i=0;i<
wordsInText.length;i++) {
          
           acronymFound =
false; //a word in text is not an acronym until proven
otherwise
           isPunctuation =
false;
          
           word =
wordsInText[i]; // pick a word from text
           lastChar =
word.charAt(word.length()-1); // get the last character of the word
form text
          
          
if(!Character.isLetter(lastChar)) { // true only if last character
of word is not a letter [a-z,A-Z]
          
    // remove the last character from word
          
    word = word.split(String.valueOf(lastChar))[0];
// by splitting word at that character and picking the first
part
          
    isPunctuation = true;
           }
           // compare word
with all acronyms to find a match if any
           for(int j=0;
j<acronyms.length;j++) {
          
    if(word.equals(acronyms[j])) { // match is
found
          
        // update result with
acronym's corresponding definition appended with a space
          
        if(isPunctuation)
          
            result +=
definitions[j]+lastChar+" "; // add punctuation (lastChar)
          
        else
          
            result +=
definitions[j]+" ";
          
       
          
        acronymFound = true;
          
        break; // for this word, stop
the search
          
    }
          
       
           }
           // case when
none of the acronyms matched the word in text
          
if(!acronymFound)
          
    result += wordsInText[i]+" "; // update result
with the actual word in text appended with a space
          
       }
       return result; // finally when
every word in text has been compared with all acronyms
      
   }
  
   // testing the method
   public static void main(String[] args) {
      
       String[] acronyms = {"JMU",
"CS"};
       String[] definitions = {"James
Madison University", "Computer Science"};
       String text = "JMU CS
rocks!";
       String result =
expandAll(acronyms,definitions,text); // call the method
      
System.out.println(result);      
   }
}
------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,
IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~