How to reverse a string using a recursive function, without
swapping or using an extra memory?
Answer Posted / smbrd
#include <iostream>
using namespace std;
void rev_str(char* str, int pos=-1, char c='\0'){
if(pos >= int(strlen(str)/2))
return;
if(c != '\0'){
str[strlen(str) - pos - 1] = str[pos];
str[pos] = c;
}
rev_str(str, ++pos, str[strlen(str) - pos - 2]);
}
int main(){
char str[] = "reverse this string";
cout << str << endl;
rev_str(str);
cout << str << endl;
//:~
return 0;
}
| Is This Answer Correct ? | 7 Yes | 15 No |
Post New Answer View All Answers
What is the use of the function in c?
What is character constants?
What is the use of header files?
What is a newline escape sequence?
What are extern variables in c?
Do you know the difference between malloc() and calloc() function?
1) There is a singing competition for children going to be conducted at a local club. Parents have been asked to arrive at least an hour before and register their children’s names with the Program Manager. Whenever a participant registers, the Program Manager has to position the name of the person in a list in alphabet order. Write a program to help the Program Manager do this by placing the name in the right place each time the Program Manger enters a name. The Logic should be written in Data Structures?
What is the stack in c?
What is the process to create increment and decrement stamen in c?
Can the curly brackets { } be used to enclose a single line of code?
What is a pointer on a pointer in c programming language?
7-Given an index k, return the kth row of the Pascal's triangle. For example, when k = 3, the row is [1,3,3,1]. For reference look at the following standard pascal’s triangle.
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 indirection?
What do mean by network ?