I want to reverse the words of a sentence, without reversing the letters in java. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I want to reverse the words of a sentence, without reversing the letters in java.

input:"I am a man" output:"man a am I"

13th Aug 2017, 8:35 AM
Bipin Babu
Bipin Babu - avatar
2 Answers
+ 1
You could try putting each word as it's own element in an array and then using a for loop to iterate through the array in reverse and printing each word whilst doing so. Some psuedocode: String arr [] = ["I","am","a,"man"]; for (int i = arr.length ()-1;i > 0;i--){ System.out.println (arr [i]); If you wanted to implement this on a more broader scale (as in any string), you could create a piece of code that iteratively puts each word into an array. For the specific string, though, that should work.
13th Aug 2017, 9:32 AM
Jacob Powell
Jacob Powell - avatar
6th Apr 2018, 6:31 AM
shubhranshu shekhar
shubhranshu shekhar - avatar