[Solved] what is the simple way to reverse the words in string in java? [duplicate]


Here is a simple solution:

    String s = "Hello stack over flow";
    List< String > words = Arrays.asList( s.split( " " ) );
    Collections.reverse( words );
    String reversed = words.get( 0 );
    for ( int i = 1; i < words.size(); ++i ) {
        reversed += " " + words.get( i );
    }

I assume that all characters except spaces are considered as part of words. You may need to provide more details if you need something else.

solved what is the simple way to reverse the words in string in java? [duplicate]