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?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the 4 types of unions?

998


How can I write functions that take a variable number of arguments?

1087


What are # preprocessor operator in c?

1057


please explain clearly about execution of c program in detail,in which stage are the printf sacnf getting into exeecutable code

2138


Why static is used in c?

1043


What is the difference between null pointer and wild pointer?

1146


List the difference between a "copy constructor" and a "assignment operator"?

967


what is different between auto and local static? why should we use local static?

1084


How are strings stored in c?

984


how we can make 3d venturing graphics on outer interface

4741


Explain what will be the outcome of the following conditional statement if the value of variable s is 10?

1198


What is array in c with example?

1179


Are negative numbers true in c?

1008


What is the most efficient way to count the number of bits which are set in an integer?

1011


Is linux written in c?

1002