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
What is %g in c?
The purpose of this exercise is to benchmark file writing and reading speed. This exercise is divided into two parts. a). Write a file character by character such that the total file size becomes approximately >10K. After writing close the file handler, open a new stream and read the file character by character. Record both times. Execute this exercise at least 4 times b). Create a buffer capable of storing 100 characters. Now after generating the characters, first store them in the buffer. Once the buffer is filled up, store all the elements in the file. Repeat the process until the total file size becomes approximately >10K.While reading read a while line, store it in buffer and once buffer gets filled up, display the whole buffer. Repeat the exercise at least 4 times with different size of buffer (50, 100, 150 …). Records the times. c). Do an analysis of the differences in times and submit it in class.
The number of bytes of storage occupied by short, int and long are a) 2, 2 and 4 b) 2, 4 and 4 c) 4, 4 and 4 d) none
Read the following data in two different files File A: aaaaaaaadddddddd bbbbbbbbeeeeeeee ccccccccffffffff File B: 11111111 22222222 33333333 By using the above files print the following output or write it in the Other file as follows aaaaaaaa11111111dddddddd bbbbbbbb22222222eeeeeeee cccccccc33333333ffffffffffff
Explain the red-black trees?
Using which language Test cases are added in .ptu file of RTRT unit testing???
List the difference between a "copy constructor" and a "assignment operator"?
When I set a float variable to, say, 3.1, why is printf printing it as 3.0999999?
What is nested structure with example?
What is the process of writing the null pointer?
How are strings stored in c?
exit () is used to a) exit () terminates the execution of the program itself b) exit () terminates the execution of the loop c) exit () terminates the execution of the block d) none of the above
What is the use of typedef in structure in c?
What is void pointers in c?
Explain function?