Function Palindrome, iterative Learn programming Java

Lesson:

Functions


Exercise:

Function Palindrome, iterative


Objetive:

Create an iterative 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)
	{
		text = text.toUpperCase();

		int begin = 0;
		int end = text.length() - 1;

		for (begin = 0; begin < end; begin++)
		{
			if (text.charAt(begin) != text.charAt(end))
			{
				return false;
			}
			end--;
		}

		return true;
	}

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

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