How to reverse a string using a recursive function, without
swapping or using an extra memory?
Answers were Sorted based on User's Feedback
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 |
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 |
Answer / 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 |
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 |
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 |
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 |
Answer / asdf
void reverse_string(char *string) {
if (*str) {
reverse_string(++str);
str--;
printf("%c",*str);
}
Is This Answer Correct ? | 2 Yes | 14 No |
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 |
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 |
Write a C program to fill a rectangle using window scrolling
Using which language Test cases are added in .ptu file of RTRT unit testing???
Is malloc memset faster than calloc?
nic scientist exam
Magic square
What is bash c?
What is sizeof in c?
Diff between for loop and while loop?
What does sizeof function do?
write a program to swap two variables a=5 , b= 10 without using third variable
What are the 5 elements of structure?
hi friends how r u as soon in satyam my interview is start but i m very confusued ta wat i do plz help me frndz wat can i do plz tell me some question and answers related with "C" which r asked in the interview .
0 Answers Aegis, CDAC, Infosys,