1. Given a string, the string is reversed by word, for example, given "this is a sentence", the output is "sentence-a", in order to simplify the problem, the string does not contain punctuation.
In two steps
1 Get "Siht si a Ecnetnes" by the reverse order of the words first
2 again the whole sentence in reverse order to get "sentence A is this"
Public class test {public static void main (String[] args) {//Scanner in = new scanner (system.in);//string str = new string ();//while ( In.hasnext ()) {////str= in.nextline (); string str = "HELLO&NBSP;WORLD&NBSP;GIRL&NBSP;ABCD"; char[] chs = str.tochararray (); Reversewords (Chs, 0, str.length ()-1); Int left = 0,right;for (int i = 0; i<str.length (); i++) {if (chs[i] == ' ') {right = i-1;reversewords (CHS, left , right); left = i+1;} Else{right = i;} if (Right == str.length ()-1) {reversewords (chs, left , right);}} for (Char s:chs) {System.out.print (s);} }}public static void reversewords (CHAR[]&NBSP;STR,&NBSP;INT&NBSP;I,&NBSP;INT&NBSP;J) {while (I <J) {char temp = str[i];str[i] = str[j];str[j] = temp;i++;j--;}}
Algorithm surface Questions