Why is my code not working?

c
code

#1

QUES=> Write a recursive function using pointers to determine whether a string is palindrome, Capitalization and spacing are ignored. Test your with the following two palindromes and at least one case that is not a palindrome.

CODE=>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void rev(char *a,int l);

void comp(char *b,char *a);

void main()

{

int l;

char x[20];

clrscr();

printf(“ENTER THE STRING:”);

gets(x);

l=strlen(x);

//printf("%d",l);

rev(&x[0],l-1);

}

void rev(char a,int l)

{

int i;

char b;

if(l==0)

{

(b)=(a);

//for(i=0;i<l;i++)

//{

printf("%c",
(b));

//}

comp(b,a);

}

else

{

(b+l)=(a+l);

//for(i=0;i<l;i++)

//{

printf("%c",
(b+l));

//}

rev(a,l-1);

}

}

void comp(char b,char a)

{

int l1=strlen(b),l2=strlen(a),count=0;

int i,j;

printf("%d %d",l1,l2);

for(i=0;i<l2;i++)

{

if(
(b+i)!=
(a+l1-i-1))

{

count++;

break;

}

}

if(count==0)

printf(“STRING IS PALINDROME”);

else

printf(“STRING IS NOT PALINDROME”);

getch();

}


#2

What is the problem you are facing with your code, can you be more specific?

In your code, after

l=strlen(x);

Write something like:

if( test_palindrome(&x[0], &x[l-1]) )
  printf("STRING IS PALINDROME");
else
  printf("STRING IS NOT PALINDROME");

Now your problem reduces to verifying whether a string is palindrome or not.
Following is algorithm for this:
function test_palindrome ( start_ptr, end_ptr )

  1. if start_prt < end_ptr, perform steps 2 and 3. Else jump to step 4.
  2. While start_ptr is a space, ignore it and increment start_ptr.
  3. While end_ptr is a space, ignore it and decrement end_ptr
  4. if start_ptr is equal to greater than end_ptr, return 1;
  5. if contents(character ignoring upper/lower case) at locations start_ptr are not equal to contents at end_ptr, return 0.
  6. if contents at both locations are same return test_palindrome(start_ptr + 1, end_ptr - 1)

Now try to implment this. Make some input sets and verify whether above algorithms is correct and not.
If you are stuck somewhere, ask the specific question. I am sure many people would be eager to give you a hand, as everybody on this forum want each other solving problems and running bug free codes and writing (not getting) bug free codes.

I have intentionally left an array bound check bug in steps 2 and 3 of above algo which would get hit when your string is small. Try to find it and fix it.

One more suggestion: it seems like you are using turbo C. It would be a good idea to use a better compiler.
You can find a good list @

  1. http://discuss.codechef.com/questions/2783/what-is-the-best-compiler-for-c
  2. http://en.wikipedia.org/wiki/List_of_compilers#C_compilers
  3. I would suggest gcc in linux environment or MSVC express edition in windows environment. (Both are free)

#3

First of all, your code is not at all readable. There may be more than one problems in your code, one of them, in my opinion, is that “rev” function receives &x[0] (which is a character pointer) as a character “a”. This mistake is present in many other places in your code (like, “char b” should be “char *b” etc.).


#4

Please at least format the code when you copied it from the internet. It smells like homework you have to do…