How to reverse a string using a recursive function, without
swapping or using an extra memory?
Answer Posted / rafael christ
#include <iostream>
using namespace std;
char* reverse_str(char* s)
{
char* reverse = new char[1];
//char* reverse;
int i;
if(*s != '\0')
reverse = reverse_str(s+1);
i = strlen(s) - 1;
if (i >= 0)
reverse[i] = s[0];
return reverse;
}
int main(void)
{
char* str = "tsirhc oraivur odraude leafar";
cout << "original:" << endl;
cout << str << endl << endl;
cout << "reversed:" << endl;
cout << reverse_str(str) << endl;
return 0;
}
| Is This Answer Correct ? | 14 Yes | 25 No |
Post New Answer View All Answers
a=10;b= 5;c=3;d=3; if(a printf(%d %d %d %d a,b,c,d) else printf("%d %d %d %d a,b,c,d);
What is the difference between printf and scanf in c?
Which is better oop or procedural?
write a program to display all prime numbers
What happens if header file is included twice?
Write a C program in Fibonacci series.
What is array in C
What is sizeof int?
Write a program to generate random numbers in c?
What are inbuilt functions in c?
Why is C language being considered a middle level language?
What is the difference between test design and test case design?
What is structure of c program?
What is quick sort in c?
What are the functions to open and close the file in c language?