How to reverse a string using a recursive function, without
swapping or using an extra memory?

Answer Posted / asif

#include <stdio.h>
#include <string.h>

void reverse(char *str)
{
if (*str == '\0')
return;

reverse(str+1);

printf("%c", *str);
}

int main()
{
char str[50];
char *ptr;
printf("Enter the string: ");
//scanf("%s", str);
fgets(str,50,stdin);
ptr = strchr(str,'\n');
*ptr = '\0';

printf("Reversed string: ");
reverse(str);
printf("\n");

return 1;
}

Is This Answer Correct ?    2 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what are non standard function in c

1680


Explain zero based addressing.

816


What is new line escape sequence?

1048


What does double pointer mean in c?

809


What are the __date__ and __time__ preprocessor commands?

815


Explain output of printf("Hello World"-'A'+'B'); ?

1209


How do you declare a variable that will hold string values?

954


Explain what is the heap?

828


What is the difference between %d and %i?

855


Why is c called "mother" language?

1087


Why can’t we compare structures?

1069


How can I split up a string into whitespace-separated fields?

838


Is stack a keyword in c?

865


Explain union. What are its advantages?

844


Write a code to generate divisors of an integer?

847