How to reverse a string using a recursive function, without
swapping or using an extra memory?

Answer Posted / smbrd

&#65279;#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


Please Help Members By Posting Answers For Below Questions

What are the types of assignment statements?

626


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

608


How many types of errors are there in c language? Explain

565


What are linked lists in c?

645


What is a scope resolution operator in c?

745






Explain goto?

709


What is main () in c?

584


Explain enumerated types in c language?

602


the portion of a computer program within which the definition of the variable remains unchanged a) mode b) module c) scope d) none

639


program to find out date after adding 31 days to a date in the month of febraury also consider the leap year

2570


What functions are used for dynamic memory allocation in c language?

597


What does the message "automatic aggregate intialization is an ansi feature" mean?

689


List the difference between a While & Do While loops?

629


What is string length in c?

605


What is the correct declaration of main?

675