9 Replies - 3482 Views - Last Post: 21 April 2011 - 02:58 PM Rate Topic: -----

#1 Hunterhod  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 21-April 11

Reversing a string using only pointers

Posted 21 April 2011 - 11:00 AM

Hey everyone! First time poster here, so I'll try to be brief.

I'd like to know how to reverse the sequence of characters in a string while only using pointers. This seems trivial enough, but I've been scratching my head over it for a little while now.

My thought was to have two pointers and a c-string. Initially, the first pointer points to the location of the first character in the string, while the second pointer points to the location of the last character in the string. Before writing a loop, I first attempted to copy the value from the first pointer to the second in order to verify that such an assignment would be possible. However, I encountered an "access violation" error during runtime.

My build thus far:
    #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;
        }
    }



Any constructive comments would be greatly appreciated!
Thanks!
-Hunter

Is This A Good Question/Topic? 0
  • +

Replies To: Reversing a string using only pointers

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Reversing a string using only pointers

Posted 21 April 2011 - 11:36 AM

1) Why are you using classes for this? It could be done much easier with a function
2)
front = new char;
end = new char;


Why are you dynamically allocating memory to these pointers? they never use it and it gets lost in a memory leak, so you might as well have just gone
front = 0;
end = 0;



3)
It is crashing because cStr is pointing to a string literal, meaning that the memory can not be changed, you can get around this by using arrays which work well with pointers.

This post has been edited by sk1v3r: 21 April 2011 - 12:06 PM

Was This Post Helpful? 1
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Reversing a string using only pointers

Posted 21 April 2011 - 11:47 AM

I think you're hugely over-complicating things and missing the mark by quite a long stretch. There's no need for new, and there's not really any need for a class either - a simple function should be able to do this.

If you can reverse a sequence using array subscripting notation, then reversing a sequence using pointers is exactly the same, just using very slightly different syntax (i.e you replace a numeric "index" value with a pointer instead).

Array subscripting syntax is inter-changable with pointer syntax (That doesn't mean arrays and pointers are the same thing, it just means that your compiler interprets the syntax in the same way).

e.g. given an array -
char my_array[10]; 


Then the following two statements are equivalent
char* begin = &my_array[0];
char* begin = my_array + 0;

You know that the first element of a "C string" is at my_array[0]
and you know that the end of a "C string" is at my_array[strlen(my_array) -1]

pointers can iterate to the next element of an array with the ++ operator or using += 1.

From there, you should be able to easily translate an array-subscripting version to a pointer-syntax version.

This post has been edited by Bench: 21 April 2011 - 11:49 AM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5760
  • View blog
  • Posts: 12,573
  • Joined: 16-October 07

Re: Reversing a string using only pointers

Posted 21 April 2011 - 11:47 AM

Don't typedef pointers! It's confusing and makes my brain hurt.

It also hides what's going on:
char *cStr = "aDb3EfgZ";



What you have done here is pointed to a "string literal" or, in this case, a c-string literal. The value pointed to cannot be changed!

If you wish to allocate a value that can be changed, try:
char cStr[] = "aDb3EfgZ";



Do not assume you know how long the string is (for(int i=0;i<4). Use strlen or just spin until you hit null.
Was This Post Helpful? 1
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Reversing a string using only pointers

Posted 21 April 2011 - 11:55 AM

You simply need a function that iterates over the array and does a simple swap of first and last, then 2nd and 2nd to last etc...
Was This Post Helpful? 1
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Reversing a string using only pointers

Posted 21 April 2011 - 12:39 PM

You started out wanting to reverse a string. A string::iterator is the thing to use to manipulate a string, and it works pretty much the same as a pointer. So rather than copying the string to a c-string and reversing that, will you settle for this?

This post has been edited by r.stiltskin: 21 April 2011 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Reversing a string using only pointers

Posted 21 April 2011 - 12:41 PM

Probably not as its probably an assignment and he needs to use pointers.

View Postr.stiltskin, on 21 April 2011 - 12:39 PM, said:

You started out wanting to reverse a string. A string::iterator is the thing to use to manipulate a string, and it works pretty much the same as a pointer. So rather than copying the string to a c-string and reversing that, will you settle for this?

*removed code*


edit by ishkabible: removed code for assigment

This post has been edited by ishkabible: 21 April 2011 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Reversing a string using only pointers

Posted 21 April 2011 - 12:48 PM

here is an algorithm

*let S be the string to be reversed.
*let A be a pointer the the first element
*let B be a pointer the the last element
*while A dose not point the same element and is not past B
--*swap A and B
--*increment A
--*increment B

this should only take O(n/2) time because it only goes to the center of the string.

This post has been edited by ishkabible: 21 April 2011 - 12:59 PM

Was This Post Helpful? 1
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Reversing a string using only pointers

Posted 21 April 2011 - 12:52 PM

View PostJaKWaC, on 21 April 2011 - 02:41 PM, said:

Probably not as its probably an assignment and he needs to use pointers.

Possibly. I didn't think so, though. That would be a very poorly-thought-out assignment since strings (read "c++ strings") are not meant to be altered internally by pointers.

Still, I suppose teachers have given stupider assignments.

But if you think it's an assignment, why don't you delete my code from the quote in your post.

This post has been edited by r.stiltskin: 21 April 2011 - 12:56 PM

Was This Post Helpful? 0
  • +
  • -

#10 Hunterhod  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 21-April 11

Re: Reversing a string using only pointers

Posted 21 April 2011 - 02:58 PM

Appreciate the help! I'm so hard wired for OOP that I sometimes forget that a simple function will do. Copying the string literal solved everything, and I've implemented a working algorithm based on your replies.
The problem came from an exam that I took a few weeks ago. I've been needing to brush up on pointers for finals.

Thanks again everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1