Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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])
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 14 2002
Added on Feb 7 2002
8 comments
298 views