0

I'm working on an assignment that requires myself to reverse a sequence of characters, which can be of any given type, using a pointer to the "front" of the sequence and a pointer to the "end" of the sequence.

In my current build, I begin by first attempting to switch the "front" and "end" characters. However, I receive an "access violation" during runtime.

My code at the moment:

#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

class StrReverse
{
public:
    StrReverse(); //default constructor

    void revStr(); //reverses a given c-string
private:
    typedef char* CharPtr;
    CharPtr front;
    CharPtr end;
    CharPtr cStr;
};

int main()
{
    StrReverse temp = StrReverse();
    temp.revStr();
    system("pause");

    return 0;
}

//default constructor
StrReverse::StrReverse()
{
    cStr = "aDb3EfgZ";
    front = new char;
    end = new char;
}

//reverses a given string
void StrReverse::revStr()
{
    for(int i = 0;i < 4;i++)
    {
        front = (cStr + i);
        end = (cStr + (7 - i));
        *front = *end;
    }
}

The key restriction with this problem is that the reversal must be done using pointers. I realize that simply reversing a string is trivial, but this restriction has me scratching my head. Any constructive comments would be greatly appreciated!

sth
  • 222,467
  • 53
  • 283
  • 367
Hunterhod
  • 185
  • 1
  • 2
  • 11
  • 3
    Why are you making a class out of this? Reverse string is a simple action, it should be a function. – Benjamin Lindley Apr 21 '11 at 17:46
  • Beginner's mistake, my experience with C++ has been dominated by OOP. I've just shortened it into one function now though, thanks for the help! – Hunterhod Apr 21 '11 at 18:25

4 Answers4

2

You assign the string literal "aDb3EfgZ" to cStr, and string literals can't be modified. Your compiler most likely stores the string literal in read only memory, and when you try to write to *front you get an access violation because of that.

To get a modifiable string, make a copy of the literal. For example:

const char *cLit = "aDb3EfgZ";
cStr = new char[strlen(cLit)+1];
strcpy(cStr, cLit);

For further detail see for example this question and the ones mentioned there in the "Linked" section.

Community
  • 1
  • 1
sth
  • 222,467
  • 53
  • 283
  • 367
  • 1
    Or just `char cStr[] = "aDb3EfgZ";` – James Kanze Apr 21 '11 at 17:51
  • I presume that that's for the algorithm, not for the data. `"aDb3EfgZ"` is an array, regardless of how you cut it; and what I would hope is that he'll call `reverse( cStr, cStr + strlen(cStr) )` to invoke the algorithm. S – James Kanze Apr 21 '11 at 18:02
1

There are several problems with your code. For starters, why the class; this is something I'd expect to be done with a simple function:

void reverse( char* begin, char* end );

And you don't need an index, since you've got the pointers already; you can just increment and decrement the pointers.

Also, why do you allocate memory in your constructor. Memory that you never use (or free).

Finally, you don't really inverse anything in your loop. You need to swap the characters, not just copy the one at the end into the one at the beginning.

And as for the access violation: a string literal is a constant. You can't modify it. If you want to do the reverse in place, you'll need to copy the string somewhere else (or use it to initialize an array).

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • I did away with the class and shortened the code to one function now, beginners mistake. I had planned to write a destructor, but the chief obstacle was the access violation. However, copying the string has fixed everything, thanks for your help! – Hunterhod Apr 21 '11 at 18:41
0

Your constructor is gonna leak memory because you loose the pointers you allocate the front and end, those allocations aren't even needed. As for your problem, you can loop though the string to find the end using while(*endptr) endptr++;, from there the size of the string is endptr - startptr; which you use to allocate a temp buffer so you can do while(startptr != endptr) *tempbuf++ = *endptr--; then free the old string and set the temp buffer as the new string

Necrolis
  • 25,836
  • 3
  • 63
  • 101
0

The basic technique for an in-place reversal:

  • get a pointer (call it 'left') to the first character in the string.
  • get a pointer (call it 'right') to the last character in the string (not counting the trailing NUL character)
  • while the left pointer is less than the right pointer
    • swap the characters located by each pointer
    • increment the left pointer
    • decrement the right pointer

That's about all there is to it. Production of a reversed copy of the string requires a bit more work.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135