# VBForums CodeBank > CodeBank - Java / J# >  Java - Reverse String using recursion

## System_Error

This is a method I wrote using recursion to reverse a string.  Recursions really not the best solution for anything, but this one turned out pretty cool.



```
public class ReverseString
{
 public String reverse(String arg)
 {
	String tmp = null;
	if (arg.length() == 1)
	{
		return arg;
	}
	
	else
	{
	
		String lastChar = arg.substring(arg.length()-1,arg.length());
		
		String remainingString = arg.substring(0, arg.length() -1);

		tmp = lastChar + reverse(remainingString);
		return tmp;
		
		
	}
  }
}
```



```
import java.io.*;


public class TestReverse
{
	public static void main(String[] args) throws IOException
 	{
		System.out.println("Enter a line to be reversed");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inData;
		inData = br.readLine();
		ReverseString rs = new ReverseString();
		System.out.println("Reversed: " + rs.reverse(inData));	
	}
}
```

----------


## crptcblade

Nice, but probably more efficient this way...


```
private static String reverseString(String s) 
{
  return (new StringBuffer(s)).reverse().toString();
}

```

----------


## System_Error

Yes, but that isn't much fun.

----------


## venture

Just did this one. Enter text and recursion method will reverse it.



```
 
import java.util.Scanner;

public class recursionDemo{

	public static void main (String[] args){
	
		Scanner scan = new Scanner (System.in);
		
		System.out.println("Enter text to reverse: ");
		String s = scan.nextLine();
		reverse(s);
		
	}
		
	public static void reverse(String s){
	
		if (s.length() == 1){
			System.out.print(s);
		}
		else{
			 
			reverse (s.substring(1, s.length()));  
			System.out.print(s.substring(0,1));

		}
	}
	
}
```

----------

