Q.Using recursive function,reverse a string.

Solution I tried

#include<stdio.h>
#include<string.h>
char reverse(char *ptr,int length)
{
return ptr[0]=ptr[length-1];
while(*ptr!='\0')
reverse(ptr+1,length-2);
}
int main(void)
{
char string[40],answer;
printf("Enter string\n");
gets(string);
answer=reverse(string,strlen(string));
printf("%c",answer);
return 0;
}

It gives an error sayin 'Unreachable code in function reverse(char near*,int)' because of the line while(*ptr!='\0') Help plz! Would really appreciate if you could make corrections in my code instead of providing me with a readymade one.

Recommended Answers

All 13 Replies

>return ptr[0]=ptr[length-1];
You always return here, so the two lines after this can never run.

>while(*ptr!='\0')
If you have a loop in a simple recursive function, it's probably wrong. Remember that you're using the recursion to hit each element in the array, so that loop should be changed to an if and used as the base case.

#include<stdio.h>
#include<string.h>
char reverse(char *ptr,int length)
{
if(*ptr=ptr[0])
return ptr[0]=ptr[length-1];
if(*ptr!='\0')
return reverse(ptr+1,length-2);
}
int main(void)
{
char string[40],answer;
printf("Enter string\n");
gets(string);
answer=reverse(string,strlen(string));
printf("%c",answer);
return 0;
}

What about this? I need a solution asap.My exam date is very close :(

Why does reverse return anything anyway - it's no use just printing a char.

You're reversing a string "in place", so the real answer is in string.
printf("%s",string);

if ( length > 1 ) {
  ptr[0]=ptr[length-1];
  reverse(ptr+1,length-2);
}

Is a closer answer which might give you the clue to the real answer.

> gets(string);
Very poor - use fgets()

I can't get it! I tried using the code u gave ,making a few changes but only the last character of the last word gets printed! Plz help! :(

You're reversing a string "in place", so the real answer is in string.
printf("%s",string);

if ( length > 1 ) {
  ptr[0]=ptr[length-1];
  reverse(ptr+1,length-2);
}

Just a thought, shouldn't you be saving the contents of ptr[0] to a temporary variable, and then copying this back to ptr[length-1] ?

#include<stdio.h>

void reverse(char *ptr, char  ch, int length)
{
   if(length != 0)
   {
      reverse(ptr, *(ptr+(strlen(ptr))-length ), (length -1));
      *(ptr+length) = ch;
   }
}
int main(void)
{
   char string[40];
   printf("Enter string\n");
   gets(string);
   reverse(string,*string,strlen(string) - 1);
   printf("%s",string);
   
   getchar();
   return 0;
}

Aia, the above solution is overly complicated, not to mention doesn't work. Its always recommended to initialize the char array to '\0' and not to use gets().

Aia, the above solution is overly complicated, not to mention doesn't work. Its always recommended to initialize the char array to '\0' and not to use gets().

Salem told the OP about gets() already. I use his/her main withouth
changing that get.
I honestly thougth that I had it. Where did I go wrong?. I'm trying
not to swap. Could you tell me?.

commented: gets? -2

>Salem told the OP about gets() already.
And so you should set a good example, and not use it in your code that you post. If you're not going to change the latter section of the code, just don't post it. Otherwise you are indirectly telling the OP that the code he/she posted is completely OK.

>Where did I go wrong?
I just glanced at it. I don't know, but it looks way over-complex. There's no need for code to look that way.

>I'm trying not to swap.
What's the point of that?

commented: Thank you +2
/*
 * reverse_test.c
 *
 */
#include <stdio.h>

reverse_it(char *ptr, char ch, int length )
{
   if( length != 0 )
   {
      reverse_it(ptr, *(ptr+(strlen(ptr)) - length), length -1);
   }
   *(ptr+length) = ch;
   
}

int main( void )
{
   char string[] = "I am not who you think";
   
   printf("Text before reversed: %s\n", string);
   
   reverse_it(string, *(string), strlen(string)-1);
   
   printf("Text after reversed: %s\n", string);
   
   getchar();
   return 0;
}

/* output:
Text before reversed: I am not who you think
Text after reversed: kniht uoy ohw ton ma I
*/

I know where I went wrong. I made a mistake with the {} of the if, inside of the function.

> I just glanced at it. I don't know, but it looks way over-complex. There's no need for code to look that way.

Is not that complicated. Is just a recursive. And I didn't want to use a traditional temp to swap.

>Is not that complicated. Is just a recursive.
Well, for one thing, you've added another parameter to the function. And for what gain? Now not only does the inside of the function look more complex, but the outside (the arguments) have a needless argument.

Aia, I'm not criticizing you for being creative, but the time to be creative is probably not when you're trying to help someone fix their code. The swap method is by far easier to comprehend than splitting up the string in the stack.

but the time to be creative is probably not when you're trying to help someone fix their code

Fair enough.

And just a passing thought, I would stay away from using 'string' as a variable name. Converting that program from C to C++ would be a maintenance headache.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.