1 PackageShb.java.demo;2 3 Public classMyTest {4 Public Static voidMain (string[] args) {5String string = "for God so loved the world kdkdk";6 reverse_2 (string);7 }8 //Method One9 Public Static voidreverse (String str) {Ten OneString[] strings = Str.split (""); A for(inti= 0; I < strings.length;i++){ -String Top =strings[0]; - the for(intj=1;j<strings.length-i;j++){ - -strings[j-1]=Strings[j]; - + } -strings[strings.length-i-1]=top; + A } at for(String string:strings) { - System.out.println (string); - } - } - //Method Two - Public Static voidreverse_2 (String str_2) { inString []strings = Str_2.split (""); -String temp = ""; to if(strings.length%2==0){ + for(inti=0;i<strings.length/2;i++){ -temp =Strings[i]; theStrings[i] = strings[strings.length-1-i]; *Strings[strings.length-1-i] =temp; $ } Panax Notoginseng for(String string2:strings) { - System.out.println (string2); the } +}Else { A for(inti=0;i< (strings.length-1)/2;i++){ thetemp =Strings[i]; +Strings[i] = strings[strings.length-1-i]; -Strings[strings.length-1-i] =temp; $ } $ for(String string2:strings) { - System.out.println (string2); - } the } - }Wuyi}
The reverse output of the implementation string of Java surface question