Palindrome, recursive Learn programming Java

Lesson:

Functions


Exercise:

Palindrome, recursive


Objetive:

Create a recursive function to say whether a string is symmetric (a palindrome). For example, "RADAR" is a palindrome.


Code:

public class Main
{
	public static boolean IsPalindrome(String text)
	{
		if (text.length() <= 1)
		{
			return true;
		}
		else
		{
			if (text.charAt(0) != text.charAt(text.length() - 1))
			{
				return false;
			}
			else
			{
				return IsPalindrome(text.substring(1, 1 + text.length() - 2));
			}
		}
	}

	public static void main(String[] args)
	{
		System.out.println(IsPalindrome("radar"));
		System.out.println(IsPalindrome("pato"));
	}
}

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