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 assignment statements?
c language supports bitwise operations, why a) 'c' language is system oriented b) 'c' language is problem oriented c) 'c' language is middle level language d) all the above
How many types of errors are there in c language? Explain
What are linked lists in c?
What is a scope resolution operator in c?
Explain goto?
What is main () in c?
Explain enumerated types in c language?
the portion of a computer program within which the definition of the variable remains unchanged a) mode b) module c) scope d) none
program to find out date after adding 31 days to a date in the month of febraury also consider the leap year
What functions are used for dynamic memory allocation in c language?
What does the message "automatic aggregate intialization is an ansi feature" mean?
List the difference between a While & Do While loops?
What is string length in c?
What is the correct declaration of main?