How To Reverse Each Word Of A String In Java?

Example :

If Input is = "Java"

Output should be = "avaj"

How??

-> Split the given inputString into words using split() method. Then take each individual word, reverse it and append to reverseString. Finally print reverseString.

Java Code :-

public class ReverseEachWord
{
    static void reverseEachWordOfString(String inputString)
    {
        String[] words = inputString.split(" ");
        
        String reverseString = "";
        
        for (int i = 0; i < words.length; i++)
        {
            String word = words[i];
            
            String reverseWord = "";
            
            for (int j = word.length()-1; j >= 0; j--)
            {
                reverseWord = reverseWord + word.charAt(j);
            }
            
            reverseString = reverseString + reverseWord + " ";
        }
        
        System.out.println(inputString);
        
        System.out.println(reverseString);
        
        System.out.println("-------------------------");
    }
    
    public static void main(String[] args)
    {
        reverseEachWordOfString("Java");
        
        reverseEachWordOfString("Java Program");
        
        reverseEachWordOfString("I am string");
        
        reverseEachWordOfString("Reverse Me");
    }
}
OUTPUT :-
Java
avaJ
*************
Java Program
avaJ margorP
*************
I am string
I ma gnirts
*************
Reverse Me
esreveR eM
*************

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
\r\n <\/div>\r\n<\/div>\r\n","isUserRated":"0","version":"7.6.29","wc_post_id":"401","isCookiesEnabled":"1","loadLastCommentId":"0","dataFilterCallbacks":[],"phraseFilters":[],"scrollSize":"32","is_email_field_required":"1","url":"https:\/\/code-projects.org\/wp-admin\/admin-ajax.php","customAjaxUrl":"https:\/\/code-projects.org\/wp-content\/plugins\/wpdiscuz\/utils\/ajax\/wpdiscuz-ajax.php","bubbleUpdateUrl":"https:\/\/code-projects.org\/wp-json\/wpdiscuz\/v1\/update","restNonce":"a4172c9c67","is_rate_editable":"0","menu_icon":"https:\/\/code-projects.org\/wp-content\/plugins\/wpdiscuz\/assets\/img\/plugin-icon\/wpdiscuz-svg.svg","menu_icon_hover":"https:\/\/code-projects.org\/wp-content\/plugins\/wpdiscuz\/assets\/img\/plugin-icon\/wpdiscuz-svg_hover.svg"}; var wpdiscuzUCObj = {"msgConfirmDeleteComment":"Are you sure you want to delete this comment?","msgConfirmCancelSubscription":"Are you sure you want to cancel this subscription?","msgConfirmCancelFollow":"Are you sure you want to cancel this follow?","additionalTab":"0"}; -->