hi all can help me about code reverse 123 ->321. put help me

i have reversed the string here by recursion..
if u hav any doubt let me know..
The code is in C++..

#include<iostream>
using namespace std;

char rs[3];//array to store the reversed string
void rev(string s,int i)
{
       static int counter=0;

       if(i<s.size())       //use of recursion here to reverse
        rev(s,++i);
        
        rs[counter++]=s[i-1];

}

int main()
{
    string s="123";
    rev(s,0);
    for(int i=1;i<=sizeof(rs);i++)
     cout<<rs[i];
    return 0;
}

Edited 6 Years Ago by WaltP: removed fake sig

i have reversed the string here by recursion..

And what do you suppose will happen to your program if the original string has more than two characters? Hint: bomb!


If that is supposed to be a c++ why did you use a character array instead of std::string to hold the reversed string? You would have solved the above issue with a std::string.

Edited 6 Years Ago by Ancient Dragon: n/a

well if u r that smart to understand it, u must also realize that the solution was given to the exact problem... the array can always be declared according to the size of input dynamically...

P.S. if u hav a solution , post it..dont just sit around and mock at others...utilize the spare tym elsewhere... :)

And what do you suppose will happen to your program if the original string has more than two characters? Hint: bomb!


If that is supposed to be a c++ why did you use a character array instead of std::string to hold the reversed string? You would have solved the above issue with a std::string.

well if u r that smart to understand it, u must also realize that the solution was given to the exact problem... the array can always be declared according to the size of input dynamically...

P.S. if u hav a solution , post it..dont just sit around and mock at others...utilize the spare tym elsewhere... :)

No, if you have a solution, do NOT post it. Help the person figure it out for himself using guidance from your greater knowledge.

We understand "the solution was given to the exact problem" and that is the problem. People learn nothing by turning in your code for their homework.

And as the Dragon said, your program is wrong.

char rs[3];//array to store the reversed string

What if the string is "Hello"? How do you stuff 6 characters into these 3? Broken code.

Edited 6 Years Ago by WaltP: n/a

as a moderator, i totally agree wid u...shall keep this in mind in future..

by the way, this reply ws concerned with ancient dragon in order to handle the reply handed over to me by him....

i have already specified that solution has only been submitted to the specific problem...so it wud be gud if u dont take it as a general case..i shall provide a suitable answer for the general problem...

No, if you have a solution, do NOT post it. Help the person figure it out for himself using guidance from your greater knowledge.

We understand "the solution was given to the exact problem" and that is the problem. People learn nothing by turning in your code for their homework.

Edited 6 Years Ago by ayan2587: n/a

Comments
Stop speaking baby talk. WITH, YOU ARE WOULD are words, not wid u r wud. Read the Member Rules

The original poster's question little ambiguous. Did he mean to reverse a string or an integer? No one knows from the small amount of information he posted.

hii..
the general case solution is as follows..

#include<iostream>
using namespace std;

 int counter;

void rev(string s,char *rs,int i)
{
       if(i<s.size())
        rev(s,rs,++i);        
           
      rs[counter++]=s[i-1];
}

int main()
{
    string s="123";
    char *rs=new char[s.size()];
    rev(s,rs,0);

    for(int i=1;i<counter;i++)
     cout<<rs[i];
   return 0;
}

hi all can help me about code reverse 123 ->321. put help me

by the way, this reply ws concerned with ancient dragon in order to handle the reply handed over to me by him....

Of course it was, and his response was spot on. Everything he said was correct. You should have not complained, and the proper response would have been "thank you for the correction" instead.

i have already specified that solution has only been submitted to the specific problem...so it wud be gud if u dont take it as a general case..i shall provide a suitable answer for the general problem...

Doesn't matter. Your solution was still broken.

And stop talking baby talk. This is a professional English-speaking forum. wud is not a word, gud is not a word, u is not a word. Use proper English. Read the Member Rules again.

well buddy..!!
i am more interested in solving threads nd my problmes than to get into all these complaining stuff..
so just take a break..
nd by the way the general code has been posted above....

Of course it was, and his response was spot on. Everything he said was correct. You should have not complained, and the proper response would have been "thank you for the correction" instead.


Doesn't matter. Your solution was still broken.

And stop talking baby talk. This is a professional English-speaking forum. wud is not a word, gud is not a word, u is not a word. Use proper English. Read the Member Rules again.

Comments
Stop being disrespectful
This article has been dead for over six months. Start a new discussion instead.