Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,305 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 780 people online right now. Registration is fast and FREE... Join Now!




Inverting a C-string

 
Reply to this topicStart new topic

Inverting a C-string, using pointers

ibaraku
30 Dec, 2008 - 01:42 AM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 186



Thanked: 3 times
My Contributions
I am trying to invert a C string using pointers, the program inverts a string only halfway through, and leaves the other half untouched. so for example, if I enter
DreamInCode
I get
edoCnInCode

CODE

#include<iostream>
#include<string>

using namespace std;

int main() {
    string str;
    char *head, *tail, *cstr;
    int i = 0;

    cout << "Enter a string: ";
    getline(cin, str);

    cstr = new char[str.size() + 1];
    strcpy(cstr, str.c_str());

    head = &cstr[0];
    tail = &cstr[str.size() - 1];

    cout << "String inverted is: ";

    while (*head != '\0') {
        cstr[i] = *tail;
        *tail--;
        *head++;
        i++;
    }

    cout << cstr;

    cout <<"\n";

    return 0;
}



How come I can't save the inversed string into cstr???
Thanks for the help




User is offlineProfile CardPM
+Quote Post


AmitTheInfinity
RE: Inverting A C-string
30 Dec, 2008 - 02:04 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,258



Thanked: 64 times
Dream Kudos: 125
My Contributions
You were just replacing starting characters with the ending ones. Try to swap them instead.

CODE

char temp;
while (head <= tail) // this should be done only halfway of string and not for complete string, as you are swapping them now.
{
        tmp = cstr[i];
        cstr[i] = *tail;
        *tail = tmp;
        *tail--;
        *head++;
        i++;
    }


I hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Inverting A C-string
30 Dec, 2008 - 04:59 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 3,572



Thanked: 268 times
Dream Kudos: 525
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
First, this is a bad syntax "*head++", make it "head++", far less ambiguous.

Now, you have to decide which approach to take and stick with it. Do you want to swap it in place or create another string and push it there? Or, if you don't have to store it at all or just print it?

Here's an example of the last option ( using pointers ):
cpp

char *head, *tail;

head = (char *)str.c_str();
tail = head + str.size();

cout << "String inverted is: ";
while (--tail >= head) { cout << *tail; }
cout << endl;


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 06:16PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month