/* #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 #include #include #include #include #include #include #include #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='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; }