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 are the types of bitwise operator?
GIVEN A FLOATING POINT NUMBER HOW IS IT ACTUALLY STORED IN MEMORY ? CAN ANYONE EXPLAIN?? THE 32 BIT REPRESENTATION OF A FLOATING POINT NUMBER ALLOTS: 1 BIT-SIGN 8 BITS-EXPONENT 23 BITS-MANTISSA
Why cant I open a file by its explicit path?
What is difference between scanf and gets?
Write a program to show the change in position of a cursor using c
What is local and global variable in c?
What is call by reference in functions?
Explain why c is faster than c++?
What is conio h in c?
What do you understand by friend-functions? How are they used?
What does emoji p mean?
What is the difference between null pointer and wild pointer?
What does the error 'Null Pointer Assignment' mean and what causes this error?
What is putchar() function?
What are the types of type qualifiers in c?