×

# Why is my code not working?

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<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++)<br> //{
printf("%c",
(b));
//}
comp(b,a);
}
else
{
(b+l)=(a+l);
//for(i=0;i<l;i++)<br> //{
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++)<br> {
if((b+i)!=(a+l1-i-1))
{
count++;
break;
}
}
if(count==0)
printf("STRING IS PALINDROME");
else
printf("STRING IS NOT PALINDROME");
getch();
}

0★pratt
111
accept rate: 0%

19.6k349497539

1

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

(30 Jan '14, 22:05)

 0 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 ) if start_prt < end_ptr, perform steps 2 and 3. Else jump to step 4. While start_ptr is a space, ignore it and increment start_ptr. While end_ptr is a space, ignore it and decrement end_ptr if start_ptr is equal to greater than end_ptr, return 1; if contents(character ignoring upper/lower case) at locations start_ptr are not equal to contents at end_ptr, return 0. 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 @ http://discuss.codechef.com/questions/2783/what-is-the-best-compiler-for-c http://en.wikipedia.org/wiki/List_of_compilers#C_compilers I would suggest gcc in linux environment or MSVC express edition in windows environment. (Both are free) answered 31 Jan '14, 15:07 5★mjbpl 419●2●7 accept rate: 6%
 0 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.). answered 01 Feb '14, 20:40 4★snk967 184●1●1●10 accept rate: 9%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,461
×243

question asked: 30 Jan '14, 18:49

question was seen: 1,553 times

last updated: 01 Feb '14, 20:40