Thursday, July 18, 2019

Java Program 5: How to reverse given string and each word of it?

Let us understand the problem with an example, suppose "Learn Core Java" is the given string then the expected output should be "nraeL eroC avaJ". We just reverse each word and print the string.

Let us implement the logic step by step:

Step 1: Read the string and split the words using white-space as delimiter. This will return a array of Strings

Step 2: Now write a method that reverses the given word and returns it. In our case reverseWord() method.

Step 3: Use StringBuilder to construct a new string using reversed words. Each reversed word will be appended with a white-space to the string builder except last word.

Checkout the below program

Hope you understood the how to solve the above problem. Thanks for reading.

Happy Testing. Happy Learning.


Java Program 6: How to reverse a String in Java?

In this post we will discuss different methods to reverse a String in java programming. Method 1: Step 1: Read the input string using ...