Reversing a string: Iteration vs. recursion
843835Feb 7 2002 — edited Feb 14 2002/*I am trying to compare the speed of the iterative vs. recursive algorithm for reversing the order of a string.
I understand the algorithm for the iterative version, but cannot think of how to do it using recursion (using charAt() and substring().
I've written a simple program using iteration to reverse a string, but am receiving errors:
*/
import java.lang.string.*;
import java.lang.StringBuffer.*;
import java.io.*;
public class StringReverse {
public static void main(String [] argv) throws IOException{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in),1);
String readString[];
System.out.println("Enter the string to be reversed:");
readString=stdin.readLine();
}
static String[] reverse(String readString[]) {
//this is iterative
for (int i=0; i < readString.length;i++ ) {
char temp= readString;
setCharAt ( readString[(readString.length-i-1)] , readString[i]);
setCharAt ( readString[(readString.length-i-1)] , temp);
}
}
for (int i=0; i < readString.length; i++) {
System.out.println(readStrings[i])
}
}