Implement the method reverseWords This method should receive

 //Implement the method \"reverseWords\". This method should receive a sentence as string (type String or StringBuilder) //and it should return a sentence with the words being reversed.  //Example \"hi how are you\"  your method should return -> \"ih woh era uoy\" //Assume the sentence contains at least one word. The words are single-spaced with no numbers or punctuation marks. //Feel free to use class string, stringBuilder or both!  //Your name here  public class TestReverseWords{       public static void main(String []args){         String sentence = \"hi how are you\"; //or StringBuilder sentence;          //Feel free to use String or StringBuilder or both of them through your code.                  String revSentence = reverseWords(sentence); //calling the method from the main               }  //implement the method reverseWords here  } 

Solution

TestReverseWords.java

public class TestReverseWords{

public static void main(String []args){
String sentence = \"hi how are you\"; //or StringBuilder sentence;
//Feel free to use String or StringBuilder or both of them through your code.
  
String revSentence = reverseWords(sentence); //calling the method from the main
System.out.println(\"Reverse String is \"+revSentence);
}

//implement the method reverseWords here

public static String reverseWords(String sentence) {
   String words[] = sentence.split(\" \");
   StringBuilder sb = new StringBuilder();
   for(int i=0; i<words.length; i++){
       StringBuilder wordSb = new StringBuilder(words[i]);
       sb.append(wordSb.reverse());
       sb.append(\" \");
   }
   return sb.toString().trim();
}

}

Output:

Reverse String is ih woh era uoy

 //Implement the method \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site