简体   繁体   中英

Reversing a string/sequence of characters using only pointers

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!

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.

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).

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM