Reverse, recursive Learn programming Java



Lesson:

Functions


Exercise:

Reverse, recursive


Objetive:

Create a program that uses recursion to reverse a string of characters (for example, from "Hello" it would return "olleH").


Code:

public class Main
{
	public static String Reverse(String text)
	{
		if (text.length() <= 1)
		{
			return text;
		}

		char firstLetter = text.charAt(0);
		String rest = text.substring(1);


		return Reverse(rest) + firstLetter;
	}

	public static void main(String[] args)
	{
		System.out.println(Reverse("Juan"));
	}
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.