Checking for Palindrome Strings or Numbers in C Language

In this programming algorithm tutorial we will look at how to find out if a string or number is a palindrome or not. A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction. A few examples of palindrome strings are: “madam”, “dad” and “radar”.

First Source Code Example

So how would such a program look like and what string function do we need from string(.h) library?
Below you’ll find the source code of a palindrome string checking program:


#include <stdio.h>
#include <string.h>

int main() {
	char one[200] = "madam";
	char two[200];
	
	strcpy(two, one);
	strrev(two);
	
	if(strcmp(one, two) == 0)
		printf("The entered string %s is a palindrome.\n", one);
	else
		printf("The entered string %s is not a palindrome.\n", one);
	
	printf("The reverse of the string is %s.\n", two);
	return 0;
}

As you can see the string.h functions strcpy(), strrev() and strcmp() are used. The strcpy() functions to copy the input string to a second string. The strrev() function is then used to reverse this second string.

We then have two strings that we can compare with strcmp() to check if the input string is the same as the reversed string. If so, the input string is a palindrome string.

Note: this example will not work on Linux, because strrev() function doesn’t exist. Take a look at the next example on how to implement your own strrev() function on you own.

Creating strrev() function

As said before, on Linux there is no strrev() function in string.h (because it is not part of the standard library), so we have to implement it ourselves to make the first example work.


#include <stdio.h>
#include <string.h>

void reverse(char s[]) {
	int c, i , j;
	for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
		c = s[i];
		s[i] = s[j];
		s[j] = c;
	}
	return;
}

int main() {
	char a[] = "madam";
	char b[200];
	
	strcpy(b,a);
	reverse(a);
	
	if(strcmp(a, b) == 0) {
		printf("The string is a Palindrome\n");
	} else {
		printf("The string is not a Palindrome\n");
	}
	return 0;
}

We have a char array that contains the palindrome “madam”. We make a copy of this string (using strcpy() function) to b[], before we reverse a[].

The reverse() function loops through each character and reverse the order. The reverse() function will reverse in-place, so the array a[] contains the reversed string.

The last statements will compare the two strings using the strcmp() function to determine if the string is a Palindrome or not.

Palindrome Numbers

A palindrome number is the same as a palindrome string, if you reverse it, it will not change. For instance the numbers 121, 212, 454 and 12321 are palindrome numbers. Take a look at the following C example:


#include <stdio.h>

int main() {
	int number, reverse = 0, temp;
	
	printf("Enter number:");
	scanf("%d", &number);
	
	temp = number;
	
	while( temp != 0 ) {
		reverse = reverse * 10;
		reverse = reverse + temp%10;
		temp = temp / 10;
	}
	
	if ( number == reverse )
		printf("The number is a Palindrome number.\n");
	else
		printf("The number is not a Palindrome number.\n");
	
	return 0;
}

We get input from the user and save it in the variable number and this variable is stored in variable temp for later use.

Then the number is reversed and as a last step the variables number and reverse are compared to determine if they are a Palindrome number or not.

To see what is happening in the while loop, you could at some additional printf statements, for example like so:


#include <stdio.h>

int main() {
	int number, reverse = 0, temp;
	
	printf("Enter number:");
	scanf("%d", &number);
	
	temp = number;
	
	while( temp != 0 ) {
		reverse = reverse * 10;
		printf("Reverse: %d\n", reverse);
		printf("%d = %d * 10\n\n", reverse, reverse);
		
		reverse = reverse + temp%10;
		printf("Reverse2: %d\n", reverse);
		printf("%d = %d + %d\n\n", reverse, reverse, temp%10);
		
		temp = temp / 10;
		printf("Temp: %d\n", temp);
		printf("%d = %d / 10\n\n", temp/10, temp);
	}
	
	if ( number == reverse )
		printf("The number is a Palindrome number.\n");
	else
		printf("The number is not a Palindrome number.\n");
	
	return 0;
}

The result will now be something like this:


Enter number:424
Reverse: 0
0 = 0 * 10

Reverse2: 4
4 = 4 + 4

Temp: 42
4 = 42 / 10

Reverse: 40
40 = 40 * 10

Reverse2: 42
42 = 42 + 2

Temp: 4
0 = 4 / 10

Reverse: 420
420 = 420 * 10

Reverse2: 424
424 = 424 + 4

Temp: 0
0 = 0 / 10

The number is a Palindrome number.

Now you can easily follow what is happening in the while loop.

That’s all for this programming algorithm tutorial where we looked at how to find out if a string or number is Palindrome string or number.

This entry was posted in Programming Algorithms. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There are currently 5 responses to “Checking for Palindrome Strings or Numbers in C Language”

Why not let us know what you think by adding your own comment!

  1. OnceMore on April 18th, 2012:

    Great algorithm tutorial, keep them coming!

  2. Cory Gross on May 8th, 2012:

    Here is a quick one I wrote out on a test the other day. Things are so much simpler now that I completely understand recursion. I have tested this code and it works on all palindromes, regardless of whether the are characters or numbers values.

    If start >= end then we only have 1 or 0 elements and that is by definition a palindrome. It is the same forwards and backwards. Else we check if the front and back elements differ, then recurse on the next inner two elements.

    template
    bool isPalindrome(T *input, int start, int end)
    {
    if (start >= end)
    return true;
    if (input[start] != input[end])
    return false;
    else
    return isPalindrome(input, start+1, end-1);
    }

  3. soumya on December 20th, 2012:

    nice……it’s very useful site.

  4. Bellal on April 3rd, 2013:

    I need a c program to count the palindrome numbers within range .
    For example if input will be (1&10)range ,output will be the count of palindrome numbers 1,2,3,4,5,6,7,8,9 i.e: output is 9.please help me.

  5. bie on October 1st, 2013:

    Need a c prigram show whether a string is palindrom or not…donot use the string handling functions

Leave a Reply: