Javatpoint Logo
Javatpoint Logo

Reverse Middle Words of a String in Java

Print the entire string s in backward, excluding the first and last words.

Examples:

Input: Hello, welcome to JavaTpoint

Output: Hello, emoclew ot JavaTpoint

Input: I am good

Output: I ma good

Input: I am good at Java

Output: I ma doog ta Java

  • The first word is printed as usually.
  • Print the opposite of each word when it has reached its conclusion for the remaining middle words. All words-aside from the final word-will be printed in reverse.
  • Print the last phrase.

File name: ReverseWordes.java

Output:

Hello emoclew ot JavaTpoint

Complexity Analysis:

  • Space Complexity: O(L) where L represents the length of the string's longest word.
  • Time Complexity: O(n)






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA