Sunday, October 30, 2011

C++ Program to check whether a String or a Number is Palindrome using function overloading


The following C++ program uses function overloading to check if the entered string or number is palindrome using a overloaded function 'pal'.The function is overloaded by specifying two variants of attributes , the first one an integer for Number Palindrome and the second a character array for String Palindrome.
1)void pal(int n) 2)void pal(char a[])
The Complete Source Code is Provided Below.If you have any queries, Just comment on this Post.
 #include<iostream.h>  
 #include<conio.h>  
 #include<stdio.h>  
 #include<string.h>  
 //c-madeeasy.blogspot.com  
 void pal(char a[])  
 {  
 int i,j,l=0;  
 l=strlen(a);  
 int f=0;  
 for(i=0,l=l-1;a[i]!='\0';i++,l--)  
 {  
 if(a[i]!=a[l])  
 {  
 f=1;  
 break;  
 }  
 }  
 if(f==1)  
 {  
 printf("NoN Palindrome");  
 }  
 else if(f==0)  
 printf("Palindome");  
 }  
 void pal(int n)  
 {  
 int a,s=0,d;  
  a=n;  
 while(n>0)  
 {  
 d=n%10;  
 s=s*10+d;  
 n=n/10;  
 }  
 if(a==s)  
 cout<<"Palindrome";  
 else  
 cout<<"Non Palindrome";  
 }  
 void main()  
 {  
 clrscr();  
 int c=0;  
 do  
 {  
 cout<<"\n1.String Palindrome 2.Number Palindrome 3.Quit";  
 cout<<"\nEnter Choice: ";  
 cin>>c;  
 switch(c)  
 {  
 case 1:  
 cout<<"Enter the String: ";  
 char a[20];  
 cin>>a;  
 pal(a);  
 break;  
 case 2:  
 cout<<"Enter the Number: ";  
 int t;  
 cin>>t;  
 pal(t);  
 break;  
 case 3:  
 break;  
 default:  
 break;  
 }  
 }while (c!=3);  
 }  

6 comments:

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.