| Close | Back |
/* #define DEBUG */
/*To study how this program works trace F7 after defining DEBUG*/
/*or just remove the comment part "/ * * /" on the first line */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Palindrome without the help of "string.h" library functions *
* *
* -- Author Rajesh _ThE gReAt *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<graphics.h>
#include<stdlib.h>
#ifdef DEBUG
int gl_fl;
#endif
main()
{
char string[20];
char rev[20];
clrscr();
printf("\nPlease enter a string\t");
scanf("%s",string);
copystr(rev,string);
revstr(rev);
#ifdef DEBUG
printf("\nReversed string is \"%s\"",rev);
#endif
if(!compare(rev,string))
printf("\nVoila ... ! a palindrome");
else
printf("\nNot a palindrome..");
#ifdef DEBUG
printf("\ngl_fl = %d",gl_fl);
#endif
getch();
}
copystr(char *dest,char *src)
{
while((*(dest++)=*(src++)) != 0);
}
revstr(char str[20])
{
char ch;
int stlen=0,i=0,cstlen;
#ifdef DEBUG
int ite=0;
printf("\nControl transferred to revstr");
fflush(stdout);
#endif
stlen=length(str);
cstlen=--stlen;
#ifdef DEBUG
printf("\ncstlen = %d\nstlen=%d",cstlen,stlen);
#endif
while(cstlen!=(stlen/2))
{
if(cstlen==i)
break;
ch=str[cstlen];
str[cstlen]=str[i];
str[i]=ch;
cstlen--;
i++;
#ifdef DEBUG
ite++;
printf("\nIteration no %d value of i = %d value of stlen = %d string is %s\nch=%c",ite,i-1,cstlen+1,str,ch);
#endif
}
}
compare(char *str1,char *str2)
{
short fl,i=0;
#ifdef DEBUG
printf("\ntransfer passed to comapre()");
#endif
while(i<length(str1) && i<length(str2)) /*Though length(str1)==lenght(str2) to make this a general pupose function, both are tested*/
{
if((*str1==0 || *str2==0) && !(*str1==0 && *str2==0))
{
#ifdef DEBUG
gl_fl=1;
#endif
return 1;
}
else if(*str1==0 || *str2==0)
{
#ifdef DEBUG
gl_fl=4;
#endif
return fl;
}
else if(str1[i]==str2[i])
{
#ifdef DEBUG
gl_fl=2;
#endif
fl=0;
}
else if((str1[i]>='A' && str1[i]<='Z') && ((str1[i]+32)==str2[i]))
{
#ifdef DEBUG
gl_fl=2;
#endif
fl=0;
}
else if((str1[i]>='a' && str1[i]<='z') && ((str1[i]-32)==str2[i]))
{
#ifdef DEBUG
gl_fl=2;
#endif
fl=0;
}
else
{
#ifdef DEBUG
gl_fl=3;
#endif
return 1;
}
i++;
}
return fl;
}
length(char *work)
{
int stlen=0;
while(*(work++))
stlen++;
return stlen;
}