Giudoku Official Site
Palindrome

A palindrome is a word or a sentence which reads the same in both ways, without counting spaces or commas or things like that.
Here is a simple method which, without any reversing, discovers if a string is a palindrome or not. It works like this:
it compares the first and the last character and then passes to the method a substring obtained by deleting them. This way, every couple of "symmetric" characters is verified, and when the string reaches a length of 1 or less (according to the initial dimension) it returns true, because a void or 1-character string is always a palindrome!
But don't worry, the AND operator assures to us the correctness of the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class RicorsioneListe
{
	public static void main(String[] args)
	{
		String[] strings = {"Ah, Satan sees Natasha", "end simple example"};
		String[] noSpacesStrings = new String[2];
		boolean[] response = {true, false};
	
		for (int i = 0; i < strings.length; i++)
		{
			noSpacesStrings[i] = deleteSpaces(strings[i]);
			System.out.println("\"" + strings[i] + "\" is palindrome? " + palindroma(noSpacesStrings[i]) + " [correct: " + response[i] + "]");
		}
	}
	
	public static String deleteSpaces(String s)
	{
		String noSpace = "";
		
		for (int i = 0; i < s.length(); i++)
		{
			if (Character.isLetter(s.charAt(i)))
			{
				noSpace += s.charAt(i);
			}
		}
		
		return noSpace.toLowerCase();
	}
	
	public static boolean palindroma(String s)
	{
		if (s.length() <= 1)
		{
			return true;
		}
		
		return (s.charAt(0) == s.charAt(s.length() - 1) && palindroma(s.substring(1, s.length() - 1)));
	}
}