Sunday, December 20, 2009

Palindrome

Hi friends today i'm going to explain about "Palindrome". Palindrome is a string which is equal to reverse of same string. i.e., "madam" is palindrome because if you reverse the string the same string 'll come.
i'm sure that you'll understood from above what's palindrome. Now i'm going to implement in program. but note that we shouldn't use reverse string function and strcmp function. because if you use ,it's simple program.

CODING:
#include< stdio.h >
#include< conio.h >
void main()
{
char a[]="mam";
char *b;
int i=0,j=0,status=1;
clrscr();
b=&a[0];
for(i=0;*b!='\0';i++,b++);
b--;
while(a[j]!=0)
{
if(a[j]!=*b)
{
status=0;
break;
}
else
{
b--;
j++;
}
}
if(status==0)
printf("!palindrome");
else
printf("palindrome");
getch();
}

1 comments:

good attempt, but there are problems:
1.conio.h and specially clrscr() are no portable codes
2. the algo used is inefficient and making the code big for nothing.
3.b=&a[0] HUH!, why not b=a;????

Its a nice blog I enjoyed reading 4g hence I gave 30 secs to editing the code, make sure you format your code properly. These are just advices, no offence meant.

CODE:
#include< stdio.h >


int main()
{
char a[]="sas";
int i=0,j=0;
for(i=0;a[i]!='\0';i++);
while(--i>=j)
{
if(a[j]!=a[i])
{
printf("!palindrome\n");
return 0;
}
else
j++;
i--;
}
printf("palindrome\n");
return 0;
}

Post a Comment