Skip to content

Instantly share code, notes, and snippets.

@ellbur
Created December 15, 2011 20:31
Show Gist options
  • Select an option

  • Save ellbur/1482751 to your computer and use it in GitHub Desktop.

Select an option

Save ellbur/1482751 to your computer and use it in GitHub Desktop.
Reverse with recursion
public class Back {
public static void main(String[] args) {
System.out.println(backRec("cs-111"));
}
static String back(String x) {
String y = "";
for (int i=0; i<x.length(); i++) {
y = y + x.charAt(x.length()-1-i);
}
return y;
}
static String backRec(String x) {
if (x.length() > 0) {
return x.charAt(x.length()-1) + backRec(x.substring(0, x.length()-1));
}
else {
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment