This is one of the common program which I was asked to create for checking if a number or string is palindrome or not, during my college days, so today, I am going to explain it's code and provide you working sample in this article.

Palindrome meaning in C

A palindrome string is a string that reads the same backward as forward and can be of odd or even length. A palindrome number is a number which is equal to its reverse, so basically, if we have the number like "121" this number is palindrome because even if we read it from the right-hand side, again it gives us "121", similarly string like "abba" is a palindrome string.

Algorithm:

Before we create a palindrome program, let's understand what should be our procedure to check it.

  1. Ask for the number from the user.
  2. Reverse the number using temporary variable in while loop (take each digit from the beginning from end of a number to start, example for "12234" we will start checking "4" first then 3 and then 2 & so on, but this number is not a palindrome number)
  3. Check the reversed number with the original one
  4. If both are equal number is palindrome otherwise it is not a palindrome number.

Program to check is a number is palindrome in C

#include <stdio.h>
 
void main()
{
   //initialize 3 variable n= original, reversedNumber= number after reversing it
   //temp variable actas as a helper, used for reversing a number
   int n, reversedNumber = 0, temp;
 
   //Ask the user for number to check
   printf("Enter a number to check if it is a palindrome or not\n");
   //get the user
   scanf("%d",&n);
 
   //save it in temp variable
   temp = n;
 
   //loop until remaining number is not equal to 0
   while (temp != 0)
   {
      
      reversedNumber = reversedNumber * 10;
      reversedNumber = reversedNumber + temp%10;
      
      //Get last digit of number by dividing it from 10 which gives remaininder
      temp    = temp/10;
   }
 
  //check if both are same
   if (n == reversedNumber)
   {
      printf("%d is a palindrome number.\n", n);
   }
   else
   {
      printf("%d isn't a palindrome number.\n", n);
   }
 
  
}

I tried executing it on online compiler https://onlinegdb.com/Hy5Csqt1m & here is the output image

palindrome-program-in-c-min.png

Enter a number to check if it is a palindrome or not  
12321  
12321 is a palindrome number.  

Well if you are thinking if above method will work for string also, I would have to say no, for above code will work only for integer numbers, we would have to create different program for strings, but with almost same logic.

Program to check if string is palindrome  in C

We will be using same logic, just a few different things, here is the procedure

  1. Get the length of string
  2. Get middle length of string and total length
  3. Loop the string & compare last character with first, second last with second & so on....if all characters are equal string is palindrome otherwise it is not a palindrome string

Here is the working code

#include <stdio.h>
#include <string.h>
int main() {
	char text[100];
	int begin, middle, end, length = 0;
    
	printf("\nEnter a string: ");
	//get user str8ing
	gets(text);
	
	//caculatye lenth of entered text until it is '\0'
	while ( text[length] != '\0' )
	{
	      length++;
	}
	// end of string is length -1
	end = length - 1;
	
	//get mid number of total length
	middle = length/2;
	
	//now loop from starting until middle
	for ( begin = 0 ; begin < middle ; begin++ ) {
	    //check if string from right hand side is same as left hand side
	    //if not, number is not palindrom, otherwise continue
		if ( text[begin] != text[end] ) {
			printf("%s is NOT a palindrome.\n", text);
			break;
		}
		end--;
	}
	if( begin == middle )
	{
	      printf("%s is Palindrome.\n", text);
	}
	return 0;
}

Output:

Enter a string: abba                         
abba is Palindrome. 

Working sample is here https://onlinegdb.com/HkkkZjFyX

palindrome-string-in-c-program-min.png

You may also like :

Leap year program in C

Program to check if a number is prime or not in C


That's it, hope I have clarified all the things related to palindrome program in C, feel free to ask any questions related to this article, in below comment's section.