CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Feb 2004
    Posts
    22

    Reverse fucntion

    hi everyone

    please tell me where did i go wrong,Thanks first.The question:
    Write the function:
    Code:
     Void reverse(char s[], char s1[]);
    The string s and s1 must be the same size.String s1 should be come a reversed copy of string s.Ex:
    in put : My name is Jessica
    out put: caissej si eman yM


    Here what i have done so far:



    PHP Code:
    #include <iostream>
    #include <cstdlib>

    class reverse{
    public:
      
    void reset() {top = EMPTY;}
      
    void push(char c) { top++; s[top] =c;}
      
    char pop() {return s[top--];}
      
    char top_of() const { return s[top];}
      
    bool empty() const {return (top ==EMPTY);}
      
    bool full() const {return (top ==FULL);}
    private:
      
    enum {max_len =100, EMPTY =-1,FULL max_len-1};
      
    char s[max_len];
      
    int top;
    };
    int main()
    {
      
    reverse s;
      
    int i;
      
    char s1;
      
    cout << "enter a sentence",
      
    cin >> s1;
      
    s.reset();
      while (
    s1[i] && !s.full())
         
    s.push(s1[i++]);
      while  (!
    s.empty())
         
    cout << s.pop();
      
    cout << endl;
      
    system("PAUSE");
      return 
    0;


  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Ain't you suppose to write a function, void reverse(char s1[], char s[]), rather than a class?

  3. #3
    Join Date
    Feb 2004
    Posts
    22
    so you saying that i need to delete class?and write function reverse?

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Reverse fucntion

    Originally posted by tinhnho
    Write the function:
    Code:
     Void reverse(char s[], char s1[]);
    The string s and s1 must be the same size.String s1 should be come a reversed copy of string s.Ex:
    in put : My name is Jessica
    out put: caissej si eman yM
    Isn't the above stated that you need to write the function? IMHO, it isn't necessary to create a class just for the above transformation.

  5. #5
    Join Date
    Feb 2004
    Posts
    22
    hi there

    the question require me have to use this class:
    Code:
    class reverse{ 
    public: 
      void reset() {top = EMPTY;} 
      void push(char c) { top++; s[top] =c;} 
      char pop() {return s[top--];} 
      char top_of() const { return s[top];} 
      bool empty() const {return (top ==EMPTY);} 
      bool full() const {return (top ==FULL);} 
    private: 
      enum {max_len =100, EMPTY =-1,FULL = max_len-1}; 
      char s[max_len]; 
      int top; 
    };

  6. #6
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    Why on earth are you using C-Style strings in C++? I would use std::string and I believe there is an algorithm for reversing a string (not sure, but I thought there was one in the algorithm library).

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Code:
    #include <string>
    #include <algorithm>
    using namespace std;
    
    string a0("0123456789");
    string a1;
    a1.resize(a0.size()+1, '\0');
    reverse_copy(a0.begin(), a0.end(), a1.begin());

  8. #8
    Join Date
    Feb 2004
    Posts
    22
    Code:
    .............
    int main() 
    { 
      Reverse s; 
      int i=0; 
      char s1[100]; 
      cout << "enter a sentence: ", 
      cin.getline(s1, 100); 
      s.reset(); 
      while (s1[i] && !s.full()) 
         s.push(s1[i++]); 
      while  (!s.empty()) 
         cout << s.pop(); 
      cout << endl; 
      system("PAUSE");
      return 0; 
    }
    it works fine now,thanks everyone

  9. #9
    Join Date
    Feb 2004
    Posts
    138
    tinhnho,

    if you still want to use that class, you need to change your cin>>s1 into cin.getline(s1,100) or something similar that uses getline because cin will ignore white space chars

  10. #10
    Join Date
    Feb 2004
    Posts
    138
    tinhnho, if you did as what I told you, you muct be sure you enter 100 chars, or it will bring all weird chars from behind...
    You declared your max_len as 100 chars so, it will print from 99 to 0

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by tinhnho
    it works fine now,thanks everyone
    You didn't write a function that reverses the string (which was your original question). All you did was code something within the main() function that supposedly reverses the string.

    What was the purpose of the Reverse class? It doesn't do what it says. If you call a class "Reverse", you would expect that it would do the "reversing", but it doesn't. It is a stack implementation.

    I'm pointing all of these things out, since you may believe that it's working -- yes it may "functionally" work, but it doesn't meet the requirements of the assignment given.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Feb 2004
    Posts
    22
    thanks Paul McKenzie , i got what you were saying.i am working on it now.one again thank for telling me that.

  13. #13
    Join Date
    Feb 2004
    Posts
    138

    Unhappy

    tinhnho,

    Here is what I did
    Code:
    void Reverse(char s1[],char s2[]){
    
    for(int i=0,j=strlen(s1)-1; i<strlen(s1),j>=0; i++,j--)
    
    s2[i]=s1[j];}

  14. #14
    Join Date
    Aug 2002
    Posts
    78
    I suppose the professor was teaching the use of a stack to reverse things, but I don't know how much "deep understanding" will be gained by it. Plus it is an extremely klunky looking implementation.

    Charleston's solution is basically a stack-based reversal, but cutting out the middleman and using the source string itself as the stack. If one wanted to be truly pedantic, one would read in reverse out of the source string and fill the destination string in order, 0, 1, 2...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured