0% found this document useful (0 votes)
323 views

C Palindrome Program, C Program For Palindrome - Programming Simplified PDF

This document provides C code examples to check if a string or number is a palindrome. It includes programs that use string functions like strcpy and strcmp as well as a program without string functions. The document also provides a program to check palindrome numbers.

Uploaded by

Prakash K
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
323 views

C Palindrome Program, C Program For Palindrome - Programming Simplified PDF

This document provides C code examples to check if a string or number is a palindrome. It includes programs that use string functions like strcpy and strcmp as well as a program without string functions. The document also provides a program to check palindrome numbers.

Uploaded by

Prakash K
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1/18/2014

C palindrome program, c program for palindrome | Programming Simplified

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


# i n c l u d e< s t d i o . h > # i n c l u d e< s t r i n g . h > i n tm a i n ( ) { c h a ra [ 1 0 0 ] ,b [ 1 0 0 ] ; p r i n t f ( " E n t e rt h es t r i n gt oc h e c ki fi ti sap a l i n d r o m e \ n " ) ; g e t s ( a ) ; s t r c p y ( b , a ) ; s t r r e v ( b ) ; i f (s t r c m p ( a , b )= =0) p r i n t f ( " E n t e r e ds t r i n gi sap a l i n d r o m e . \ n " ) ; e l s e p r i n t f ( " E n t e r e ds t r i n gi sn o tap a l i n d r o m e . \ n " ) ; r e t u r n0 ; }

Download palindrome program. Output of program:

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

C palindrome program, c program for palindrome | Programming Simplified

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

C program for palindrome without using string functions


# i n c l u d e< s t d i o . h > # i n c l u d e< s t r i n g . h > i n tm a i n ( ) { c h a rt e x t [ 1 0 0 ] ; i n tb e g i n ,m i d d l e ,e n d ,l e n g t h=0 ; g e t s ( t e x t ) ; w h i l e(t e x t [ l e n g t h ]! =' \ 0 ') l e n g t h + + ; e n d=l e n g t h-1 ; m i d d l e=l e n g t h / 2 ; f o r (b e g i n=0;b e g i n<m i d d l e;b e g i n + +) { i f(t e x t [ b e g i n ]! =t e x t [ e n d ]) { p r i n t f ( " N o tap a l i n d r o m e . \ n " ) ; b r e a k ; } e n d ; } i f (b e g i n= =m i d d l e) p r i n t f ( " P a l i n d r o m e . \ n " ) ; r e t u r n0 ; }

C program check palindrome


# i n c l u d e< s t d i o . h > i n ti s _ p a l i n d r o m e ( c h a r * ) ; v o i dc o p y _ s t r i n g ( c h a r * ,c h a r * ) ; v o i dr e v e r s e _ s t r i n g ( c h a r * ) ; i n ts t r i n g _ l e n g t h ( c h a r * ) ; i n tc o m p a r e _ s t r i n g ( c h a r * ,c h a r * ) ; i n tm a i n ( ) { c h a rs t r i n g [ 1 0 0 ] ; i n tr e s u l t ; p r i n t f ( " E n t e ras t r i n g \ n " ) ; g e t s ( s t r i n g ) ; r e s u l t=i s _ p a l i n d r o m e ( s t r i n g ) ; i f(r e s u l t= =1) p r i n t f ( " \ " % s \ "i sap a l i n d r o m es t r i n g . \ n " ,s t r i n g ) ; e l s e p r i n t f ( " \ " % s \ "i sn o tap a l i n d r o m es t r i n g . \ n " ,s t r i n g ) ; r e t u r n0 ; } i n ti s _ p a l i n d r o m e ( c h a r* s t r i n g ) { i n tc h e c k ,l e n g t h ; c h a r* r e v e r s e ; l e n g t h=s t r i n g _ l e n g t h ( s t r i n g ) ; r e v e r s e=( c h a r * ) m a l l o c ( l e n g t h + 1 ) ;

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 palindrome program, c program for palindrome | Programming Simplified

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

C palindrome program, c program for palindrome | Programming Simplified

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

4/4

You might also like