You are not logged in. Please login at www.codechef.com to post your questions!

×

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<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++)<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();
}

asked 30 Jan '14, 18:49

pratt's gravatar image

0★pratt
111
accept rate: 0%

edited 30 Jan '14, 18:56

admin's gravatar image

0★admin ♦♦
19.0k348495533

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) betlista ♦♦3★

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)
link

answered 31 Jan '14, 15:07

mjbpl's gravatar image

5★mjbpl
41927
accept rate: 6%

edited 31 Jan '14, 15:11

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.).

link

answered 01 Feb '14, 20:40

snk967's gravatar image

4★snk967
1741110
accept rate: 9%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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,388
×228

question asked: 30 Jan '14, 18:49

question was seen: 1,500 times

last updated: 01 Feb '14, 20:40