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

Answers were Sorted based on User's Feedback



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

Answer / stephen

#include <iostream>
#include <string>
using namespace std;
char * reverse (char *); //function prototype
int length(char *); //function prptotype

int main()
{
int i;
char *str = new char[6], *rev = new char[6];
cin >> str;
strcpy(rev,reverse(str));

cout <<"Original "<< str << " reverse " << rev << endl;
free(str);
free(rev);
return 0;
}

int length(char *s)
{
int i;
for (i=0; *(s+i)!='\0' ; ++i);
return i;
}

char *reverse(char *s)
{
char *t;
int i, n;
n=length(s);
t = new char[n];
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';

return t;
}

/*based off of answer 8 i took this an intialized the
pointers so that it would run, and switched it over to the
C++ standard output commands. His algorithm was correct, he
just forgot to setup the memory*/

Is This Answer Correct ?    10 Yes 14 No

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

Answer / stephen

#include <iostream>
#include <string>
using namespace std;
char * reverse (char *); //function prototype
int length(char *); //function prptotype

int main()
{
int i;
char *str = new char[6], *rev = new char[6];
cin >> str;
strcpy(rev,reverse(str));

cout <<"Original "<< str << " reverse " << rev << endl;
free(str);
free(rev);
return 0;
}

int length(char *s)
{
int i;
for (i=0; *(s+i)!='\0' ; ++i);
return i;
}

char *reverse(char *s)
{
char *t;
int i, n;
n=length(s);
t = new char[n]; //opps have to add 1 here or there
wont be room for a null!
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';

return t;
}
//can only handle words 5 letters or less.
/*based off of answer 8 i took this an intialized the
pointers so that it would run, and switched it over to the
C++ standard output commands. His algorithm was correct, he

Is This Answer Correct ?    8 Yes 13 No

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

Answer / fgy

no ansawer

Is This Answer Correct ?    8 Yes 14 No

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

Answer / 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

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

Answer / mahesh auti

#include<stdio.h>
#include <string.h>

char * reverse (char *); //function prototype
int length(char *); //function prptotype

void main()
{
int i;
char *str, *rev;
clrscr();
gets(str);
strcpy(rev,reverse(str));

printf("Original %s Reverse %s", str, rev);
free(str);
free(rev);
getch();
}

int length(char *s)
{
int i;
for (i=0; *(s+i)!='\0' ; ++i);
return i;
}

char *reverse(char *s)
{
char *t;
int i, n;
n=length(s);
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';
printf("\nOUT: %s\n", t);
return t;
}

Is This Answer Correct ?    17 Yes 27 No

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

Answer / shivaraj

main()
{
char str[10];
cin>>str;
int len=strlen(str);
reverse(len);
cout<<"Reversed string is: "<<str;
}

void reverse(int len)
{
static int i=0;
str[i]=str[len-1]; //put the char in last pos to first pos
for(j=len-1;j>i;j--)
str[j]=str[j-1]; //shift to right
i++;

if(i==len)
return;

reverse(len);
}

Is This Answer Correct ?    5 Yes 15 No

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

Answer / 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

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

Answer / asdf

void reverse_string(char *string) {

if (*str) {
reverse_string(++str);
str--;
printf("%c",*str);
}

Is This Answer Correct ?    2 Yes 14 No

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

Answer / moinom

#include <iostream>
#include <conio>

void reverse(char a[], int s, int sc );

void reverse(char a[], int s, int sc ){

if ((sc-s)<(s-1))
{
a[sc-s]^=a[s-1];
a[s-1]^=a[sc-s];
a[sc-s]^=a[s-1];
reverse (a, s-1, sc) ;

}

}

void main (){


char a[]="ABCDEFG";

reverse(a, 7, 7);
cout<<a;
getch(); //i just use it to freeze the screen

}

Is This Answer Correct ?    14 Yes 29 No

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

Answer / boomer

void Rstring( char *str,int len)
{
for(int i = 0; i < (len/2); i++)
{
str[i] ^= str[len-i-1];
str[len-i-1] ^= str[i];
str[i] ^= str[len-i-1];
}
}

int main( void )
{
char str[] = "my string";
printf("Actual string is [%s]\n", str);
Rstring(str,strlen(str));
printf("Reversed string is [%s]\n", str);

}

I dont call this swaping, coz it's not, recursive creates
new incarnations of the reverse func, EXTRA MEMORY BIG
TIME!!!

Is This Answer Correct ?    21 Yes 40 No

Post New Answer

More C Interview Questions

Write down the program to sort the array.

4 Answers   Impiger,


What are the advantages of the functions?

0 Answers  


write a c program that prints all multiples of 3between 1 and 50.

5 Answers  


How will you print TATA alone from TATA POWER using string copy and concate commands in C?

0 Answers   Amdocs, Apps Associates,


how to get starting address of a running C program

3 Answers  






how does the C compiler interpret the following two statements p=p+x; q=q+y; a. p=p+x; q=q+y b. p=p+xq=q+y c. p=p+xq; q=q+y d. p=p+x/q=q+y

2 Answers   TCS, Tech Synergy,


What happens if a header file is included twice?

0 Answers  


How to declare a variable?

0 Answers  


Can a variable be both static and volatile in c?

0 Answers  


What is the output of below code? main() { static int a=5; printf("%3d",a--); if(a) main(); }

1 Answers  


a character variable can at a time store a) 1 character b) 8 characters c) 254 characters d) none of the above

3 Answers  


Is a pointer a kind of array?

0 Answers  


Categories