Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
3

How to reverse a sentence in Java without losing meaningof words. 

i.e. 

I live in Santa Clara

to

Clara Santa in live I

 

Rishi Yadav
11/01/2011 at 16:29
1 Answer
1
5

Basic loop takes care of that:

    private static String reverseWordOrderMethod(String original)
    {       
        String[] originalStringArray = original.split(" ");
        StringBuffer reverse = new StringBuffer();
   
        for(int i=originalStringArray.length-1; i>=0; i--)
        {
            reverse.append(originalStringArray[i]);
            reverse.append(" ");           
        }   
        return reverse.toString();
    }

Todd Reese
11/10/2011 at 10:31

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details