By Kardi Teknomo, PhD.


< Previous | Next | Content >

Testing if it is a Palindrome

To test whether a word or a number is a palindrome has been used in many puzzle as fun problems as well as in many simple programming challenges.

In making the program to test palindrome, we need to be more precise in definition of a palindrome. Here I listed in words the definition of palindrome that we can actually use as an algorithm to test palindrome.

  • If the word or number is only one digit, it is NOT a palindrome
  • If the word or number has more than 2 digits length (say, n digits), we check all the digits from the left up to the middle digit. We define left digit as one digit character or one digit number at pointer position from the left. Similarly, we define right digit as one digit character or one digit number at pointer position from the right. If the left digit is equal to the right digit, we move to the next digit. However, if we find a left digit is not equal to the right digit, we terminate the iteration with conclusion that it is not a palindrome. If all digits have been tested (except the middle digit) and we always find the match between left digit and the right digit, we conclude that the word or the number is a palindrome.

Put the palindrome definition above into VB code, we have the following code.

Function isPalindrome(s As String) As Boolean ' return true if s is a palindrome ' (c) 2011 Kardi Teknomo ' http://people.revoledu.com/kardi/ Dim n As Integer 'length of word Dim i As Integer 'counter Dim isFound As Boolean ' at first it is false Dim left As String 'letter from left Dim right As String 'letter from right n = Len(s) If n = 1 Then isPalindrome = False 'palindrome is undefined for 1 digit Exit Function Else For i = 0 To Int(n / 2) - 1 left = Mid(s, i + 1, 1) right = Mid(s, n - i, 1) If left <> right Then isFound = True Exit For End If Next i If isFound Then isPalindrome = False Else isPalindrome = True End If End If End Function

Using the code above, we can find out the following:

  • If the word or number has 2 digits length, it is a palindrome if and only if the two digits are equal
  • If the word or number has 3 digits length, it is a palindrome if and only if the first digit is equal to the last digit, regardless what the middle digit is.

In the next sections , we will use the palindrome test above to generate and to search for next palindrome.


< Previous | Next | Content >

Share and save this tutorial
Add to: Del.icio.us Add to: Digg Add to: StumbleUpon Add to: Reddit Add to: Slashdot Add to: Technorati Add to: Netscape Add to: Newsvine Add to: Mr. Wong Add to: Webnews Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Simpy Add to: Furl Add to: Yahoo Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Blinkbits Add to: Ma.Gnolia Information

These tutorial is copyrighted .

Preferable reference for this tutorial is

Teknomo, Kardi. (2012) Palindrome. http://people.revoledu.com/kardi/tutorial/Palindrome/