0

im tryin to reverse an array using pointer which is a class member:

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

class my_string
{
    char* ptr;
    int size;
public:
    my_string(){};
    my_string(char* str) : ptr(str),size(strlen(ptr)){};
    char* getstr () {return ptr;};
    void reverse();
    int find (char);
    void print();
};

void my_string::reverse()
{
     int size2=size;
     for (int i=0;i<(size/2);i++)
       {
           char tmp=ptr[i];
           ptr[i]=ptr[size2-1];
           ptr[size2-1]=ptr[i];
           size2--;
       }
}

int my_string::find(char c)
{
    for (int i=0;i<size;i++)
     {
        if (ptr[i]==c)
        return i;
     }
    return -1;
}

void my_string::print()
{
    for (int i=0;i<size;i++)
        cout<<ptr[i];
    cout<<endl;
}


int main()
{
    my_string s1("abcde");
    s1.print(); 
    s1.reverse();
    s1.print();
}

im not gettin any errors but the reverse function is surely not working. can someone please explain to me why?

*this is an homework assignment asking me not to use dynamic allocation or strings (for now).

1
  • 1
    you have size2-1, i think it should be size2-i Commented Feb 4, 2014 at 23:25

2 Answers 2

1

You didn't mention not being able to use standard library algorithms, so

std::reverse(ptr, ptr+size);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use standard algorithm std::reverse declared in header <algorithm>. For example

std::reverse( ptr, ptr + size );

But if you want to do it yourself then the function could look the following way

void my_string::reverse()
{
     for ( int i = 0; i < size/2; i++ )
     {
           char tmp = ptr[i];
           ptr[i] = ptr[size-1-i];
           ptr[size-1-i] = tmp;
     }
}

A test program

#include <iostream>
#include <cstring>

int main()
{
    char s[] = "123456789";
    char *ptr = s;
    int size = std::strlen( ptr );

    std::cout << s << std::endl;

    for ( int i = 0; i < size/2; i++ )
    {
              char tmp = ptr[i];
              ptr[i] = ptr[size-1-i];
              ptr[size-1-i] = tmp;
    }

    std::cout << s << std::endl;
}

Output is

123456789
987654321

3 Comments

i tried copying the answer u placed but it still doesnt work. i even tried using reverse with the member functions that one of them i just decared now : code char* getstr () {return ptr;}; char* getstrsize() {return ptr+size;};code
I can guess that you are using a string literal as an argument of the constructor. You may not use string literals such a way. They may not be changed.
I think that your class shall dynamically allocate memory and copy argumentl in this memory. And destructor shall free this memory. Also you need to write a copy constructor and copy assignment operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.