Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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 a program in c language that uses function to locate and return the smallest and largest integers in an array,number and their position in the array. it should also find and return the range of the numbers , that is , the difference between the largest number and the smallest.

1 Answers  


Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10

1 Answers   CMS,


What is the total generic pointer type?

0 Answers  


How can I run c program?

0 Answers  


what is difference between ANSI structure and C99 Structure?

1 Answers   Wipro,


what are the general concepts of c and c++

2 Answers  


what is use#in c

3 Answers  


What is register variable in c language?

0 Answers  


Where are the auto variables stored?

0 Answers   TISL,


main() { int a,b; printf("%d,%d",scanf("%d%d",&a,&b)); } => do u mean above program's output... =>output will be:2,whatever you enter value for b. =>because scanf is a library fn which will return how many arguements it processes, and second value you are right mr.Satya but i found my self unable to understand that for the first time scanf returns the no of successful matches but how for the second time it returns the value of 'b'.while a function should return the same 'r' value every time.

1 Answers   Cisco,


How many types of operator or there in c?

0 Answers  


Explain what is the difference between text files and binary files?

0 Answers  


Categories