Reverse a given string and print the new string. For example, given a string “John Snow” the output should be “wonS nhoJ”. using java...
Responses (1)
You could make a new method called reverse (either static or not, in this case static because it will be directly called in the main method--as seen below):
public static String reverse( String string ) {
String newString = "";
for( int i = string.length()-1; i >= 0; i--) {
newString = newString + string.charAt(i);
}
return newString;
}
To print this reversed string, you would specify in the main method:
public static void main( String[] args ) {
String string = "John Snow";
System.out.println( reverse(string) );
}