Six different easy ways to find whether a string or number is palindrome or not in C Programming

01. C program for palindrome using string function

We can check whether a number is palindrome or not using String functions. First we stores the inputted value using gets into a variable 'original' and then copies the content of variable 'original' to the variable 'reverse'. Reverse the content of variable 'reverse' using string function strrev(). After reversing the content, compare the two variables 'original' and 'reverse' using string function strcmp(). If both strings are same then strcmp() will return 0 and that means the inputted string to the variable 'original' is palindrome otherwise it is not palindrome.

 

#include <stdio.h>
#include <string.h>
 
int main()
{
   	char original[100], reverse[100];
  
    printf("Enter your string :");
   	gets(original);
 
   	strcpy(reverse, original);
   	strrev(reverse);
    if (strcmp(original, reverse) == 0) printf("%s is a palindrome.",original);
   		else printf("%s is not a palindrome.",original);
   	return 0;
}

02. Program to check whether a Number is palindrome or not

 

#include <stdio.h>
 
int main()
{
   	int original, reverse = 0, temp;
   	printf("Enter a number to check palindrome or notn");
   	scanf("%d",&original);
   	temp = original;
    while (temp != 0){
      	reverse = reverse * 10;
      	reverse = reverse + temp%10;
      	temp    = temp/10;
   	}
    if (original == reverse)  printf("%d is a palindrome number.", original);
   		else printf("%d is not a palindrome number.", original);
   return 0;
}

03. Program to check whether a string is palindrome or not without using string functions

 

#include <stdio.h>
#include <string.h>
 
int main()
{
   	char original [100];
   	int begin, middle, end, length = 0;
   	gets(original);
   	while (original [length] != '') length++;
   	end = length - 1;
   	middle = length/2;
    for (begin = 0; begin < middle; begin++){
      	if (original [begin] != original [end]){
         	printf("%s is not a palindrome.", original);
         	break;
      	}
      	end--;
   	}
   	if (begin == middle) printf("%s is Palindrome.", original);
   	return 0;
}

04. Program to check whether a Number is palindrome or not using recursion

 

#include <stdio.h>

int checkPalindrome(int number){
   	static int reverse = 0, remainder;
   	if(number != 0){
      	remainder = number % 10;
      	reverse = reverse * 10 + remainder;
      	checkPalindrome(number/10);
   	}
   	return reverse;
}

int main(){
   	int number, reverse;
   	printf("Enter a number: ");
   	scanf("%d",&num);
   	reverse = checkPalindrome(number);
   	if(number==reverse) printf("%d is a palindrome number",number);
   		else  printf("%d is not a palindrome number",number);
   	return 0;
}
	

05. C Program For finding all Palindrome numbers in the given range

 

#include <stdio.h>

int main(){
    int num,r,sum,temp;
    int min,max;

    printf("Enter the minimum range: ");
    scanf("%d",&min);
    printf("Enter the maximum range: ");
    scanf("%d",&max);

    printf("Palindrome numbers in the given range are: ");
    for(num=min;num<=max;num++){
         temp=num;
         sum=0;
         while(temp){
             r=temp%10;
             temp=temp/10;
             sum=sum*10+r;
         }
         if(num==sum) printf("%d ",num);
    }
    return 0;
}

06. Program to check whether a Number is palindrome or not using Pointers

 

#include <stdio.h>

int main()
{
 	char str[100];
 	char *prev_position,*next_position;
 	printf("Your string : ");
 	gets(str);
 	for(prev_position=str ; *prev_position!=NULL ; prev_position++);
  	for(next_position=str, prev_position-- ; prev_position>=next_position; ){
    	if(*prev_position==*next_position){
            prev_position--;
            next_position++;
        }
        else break;
      }
  	if(next_position>prev_position) printf("nPalindrome");
  	else printf("nNot a palindrome");
  	getch();
  	return 0;
}

Six different easy ways to find whether a string or number is palindrome or not using C Programming.