Write a c program to check given string is palindrome number or not

What is Palindrome Number ?

a word, phrase, or sequence that reads the same backwards as forwards

Some palindrome strings examples are "dad", "radar", "madam" etc.

How check given string is palindrome number or not ?


This program works as follows :- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not.


palindrome Program For String using String function

#include <stdio.h>
#include <conio.h>
void main() {
	char *a;
	int i,len,flag=0;
	clrscr();
	printf("\nENTER A STRING: ");
	gets(a);
	len=strlen(a);
	for (i=0;i<len;i++) {
		if(a[i]==a[len-i-1])
		     flag=flag+1;
	}
	if(flag==len)
	             printf("\nTHE STRING IS PALINDROM"); else
	             printf("\nTHE STRING IS NOT PALINDROM");
	getch();
}


Result

ENTER A STRING:Ankit
THE STRING IS NOT PALINDROM







palindrome Program For String without using String function

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







palindrome Program For Number

#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num){
         r=num%10;
         num=num/10;
         sum=sum*10+r;
    }
    if(temp==sum)
         printf("%d is a palindrome",temp);
    else
         printf("%d is not a palindrome",temp);

    return 0;
}


Result

Enter a number: 131
131 is a palindrome







C Program For find Palindrome numbers in given range

#include
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 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;
}


Result

Enter the minimum range: 1
Enter the maximum range: 50
Palindrome numbers in given range are: 1 2 3 4 5 6 7 8 9 11 22 33 44







palindrome Program For String using loop

#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    for(temp=num;num!=0;num=num/10){
         r=num%10;
         sum=sum*10+r;
    }
    if(temp==sum)
         printf("%d is a palindrome",temp);
    else
         printf("%d is not a palindrome",temp);

    return 0;
}


Result

Enter a number: 1221
1221 is a palindrome







palindrome Program using recursion

#include<stdio.h>

int checkPalindrome(int);
int main(){
    int num,sum;

    printf("Enter a number: ");
    scanf("%d",&num);

    sum = checkPalindrome(num);

    if(num==sum)
         printf("%d is a palindrome",num);
    else
    printf("%d is not a palindrome",num);

    return 0;
}

int checkPalindrome(int num){

    static int sum=0,r;

    if(num!=0){
         r=num%10;
         sum=sum*10+r;
         checkPalindrome(num/10);
    }

    return sum;
}


Result

Enter a number: 25
25 is not a palindrome







palindrome Program using Pointer

#include <stdio.h>
int is_palindrome(char*);
void copy_string(char*, char*);
void reverse_string(char*);
int string_length(char*);
int compare_string(char*, char*);
int main() {
	char string[100];
	int result;
	printf("Enter a string\n");
	gets(string);
	result = is_palindrome(string);
	if ( result == 1 )
	      printf("\"%s\" is a palindrome string.\n", string); else
	      printf("\"%s\" is not a palindrome string.\n", string);
	return 0;
}
int is_palindrome(char *string) {
	int check, length;
	char *reverse;
	length = string_length(string);
	reverse = (char*)malloc(length+1);
	copy_string(reverse, string);
	reverse_string(reverse);
	check = compare_string(string, reverse);
	free(reverse);
	if ( check == 0 )
	      return 1; else
	      return 0;
}
int string_length(char *string) {
	int length = 0;
	while(*string) {
		length++;
		string++;
	}
	return length;
}
void copy_string(char *target, char *source) {
	while(*source) {
		*target = *source;
		source++;
		target++;
	}
	*target = '\0';
}
void reverse_string(char *string) {
	int length, c;
	char *begin, *end, temp;
	length = string_length(string);
	begin = string;
	end = string;
	for ( c = 0 ; c < ( length - 1 ) ; c++ )
	       end++;
	for ( c = 0 ; c < length/2 ; c++ ) {
		temp = *end;
		*end = *begin;
		*begin = temp;
		begin++;
		end--;
	}
}
int compare_string(char *first, char *second) {
	while(*first==*second) {
		if ( *first == '\0' || *second == '\0' )
		         break;
		first++;
		second++;
	}
	if( *first == '\0' && *second == '\0' )
	      return 0; else
	      return -1;
}





Quantitative Aptitude
Reasoning
Programming
Interview