You are on page 1of 4

c palindrome program, c program for palindrome | Programming Simpl...

http://www.programmingsimplified.com/c-program-find-palindrome

Home C programming C programming examples c palindrome program, c program for palindrome

c palindrome program, c program for palindrome


C program for palindrome or palindrome in c programming: palindrome program in c language, c code to check if a string is a palindrome or not and for palindrome number. 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. To perform copy, reverse and compare operations we use strcpy, strrev and strcmp functions of string.h respectively, if you do not wish to use these functions see c programming code for palindrome without using string functions. Some palindrome strings examples are "dad", "radar", "madam" etc.

C program for palindrome


#include <stdio.h> #include <string.h> main() { char a[100], b[100]; printf("Enter the string to check if it is a palindrome\n"); gets(a); strcpy(b,a); strrev(b); if( strcmp(a,b) == 0 ) printf("Entered string is a palindrome.\n"); else printf("Entered string is not a palindrome.\n"); return 0; }

Download palindrome executable Output of program:

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

1 of 4

12/24/2012 5:42 PM

c palindrome program, c program for palindrome | Programming Simpl...

http://www.programmingsimplified.com/c-program-find-palindrome

if ( n == reverse ) printf("%d is a palindrome number.\n", n); else printf("%d is not a palindrome number.\n", n); return 0; }

C program for palindrome without using string functions


#include <stdio.h> #include <string.h> 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; }

C program check palindrome


#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*); 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; }

2 of 4

12/24/2012 5:42 PM

c palindrome program, c program for palindrome | Programming Simpl...

http://www.programmingsimplified.com/c-program-find-palindrome

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 = { temp = *end = *begin 0 ; c < length/2 ; c++ ) *end; *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; }

C programming: C programming examples

Guest (not verified)


Sun, 10/07/2011 - 15:13 permalink

awesome
awesome man! you really rock.
reply

Guest (not verified)


Sat, 30/07/2011 - 16:33 permalink

good, the program is working


good, the program is working
reply

Guest (not verified)


Sun, 14/08/2011 - 11:16 permalink

excellent

3 of 4

12/24/2012 5:42 PM

c palindrome program, c program for palindrome | Programming Simpl...

http://www.programmingsimplified.com/c-program-find-palindrome

excellent
reply

Guest (not verified)


Sun, 21/08/2011 - 22:12 permalink

One Request
Please post the same program without using library function.
reply

adminPs
Mon, 22/08/2011 - 10:16 permalink

Code added
Required c code is added to content.
reply

Guest (not verified)


Wed, 28/09/2011 - 18:14 permalink

palindrome program in c
Thanks for palindrome program in c language to check number and string as a palindrome.
reply

Guest (not verified)


Sat, 29/10/2011 - 13:26 permalink

c palindrome code
Thanks a lot!
reply

4 of 4

12/24/2012 5:42 PM

You might also like