C Palindrome Program, C Program For Palindrome - Programming Simplified PDF
C Palindrome Program, C Program For Palindrome - Programming Simplified PDF
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.
Palindrome number in c
# i n c l u d e< s t d i o . h > m a i n ( ) { i n tn ,r e v e r s e=0 ,t e m p ;
http://www.programmingsimplified.com/c-program-find-palindrome
1/4
1/18/2014
p r i n t f ( " E n t e ran u m b e rt oc h e c ki fi ti sap a l i n d r o m eo rn o t \ n " ) ; s c a n f ( " % d " , & n ) ; t e m p=n ; w h i l e (t e m p! =0) { r e v e r s e=r e v e r s e*1 0 ; r e v e r s e=r e v e r s e+t e m p % 1 0 ; t e m p=t e m p / 1 0 ; } i f(n= =r e v e r s e) p r i n t f ( " % di sap a l i n d r o m en u m b e r . \ n " ,n ) ; e l s e p r i n t f ( " % di sn o tap a l i n d r o m en u m b e r . \ n " ,n ) ; r e t u r n0 ; }
http://www.programmingsimplified.com/c-program-find-palindrome
2/4
1/18/2014
c o p y _ s t r i n g ( r e v e r s e ,s t r i n g ) ; r e v e r s e _ s t r i n g ( r e v e r s e ) ;
c h e c k=c o m p a r e _ s t r i n g ( s t r i n g ,r e v e r s e ) ; f r e e ( r e v e r s e ) ; i f(c h e c k= =0) r e t u r n1 ; e l s e r e t u r n0 ; } i n ts t r i n g _ l e n g t h ( c h a r* s t r i n g ) { i n tl e n g t h=0 ; w h i l e ( * s t r i n g ) { l e n g t h + + ; s t r i n g + + ; } r e t u r nl e n g t h ; } v o i dc o p y _ s t r i n g ( c h a r* t a r g e t ,c h a r* s o u r c e ) { w h i l e ( * s o u r c e ) { * t a r g e t=* s o u r c e ; s o u r c e + + ; t a r g e t + + ; } * t a r g e t=' \ 0 ' ; } v o i dr e v e r s e _ s t r i n g ( c h a r* s t r i n g ) { i n tl e n g t h ,c ; c h a r* b e g i n ,* e n d ,t e m p ; l e n g t h=s t r i n g _ l e n g t h ( s t r i n g ) ; b e g i n=s t r i n g ; e n d=s t r i n g ; f o r(c=0;c<(l e n g t h-1);c + +) e n d + + ; f o r(c=0;c<l e n g t h / 2;c + +) { t e m p=* e n d ; * e n d=* b e g i n ; * b e g i n=t e m p ; b e g i n + + ; e n d ; } } i n tc o m p a r e _ s t r i n g ( c h a r* f i r s t ,c h a r* s e c o n d ) { w h i l e ( * f i r s t = = * s e c o n d ) { i f(* f i r s t= =' \ 0 '| |* s e c o n d= =' \ 0 ') b r e a k ; f i r s t + + ; s e c o n d + + ; } i f (* f i r s t= =' \ 0 '& &* s e c o n d= =' \ 0 ') r e t u r n0 ; e l s e r e t u r n1 ; }
Pointers are used in functions, you can develop your code without using pointers.
http://www.programmingsimplified.com/c-program-find-palindrome
3/4
1/18/2014
http://www.programmingsimplified.com/c-program-find-palindrome
4/4