Simple Sentence Reverser in C++
<p>I'm trying to build a program to solve a problem in a text book I bought recently and it's just driving me crazy.</p>

<p>I have to built a sentence reverser so I get the following:</p>

<p>Input = "Do or do not, there is no try."
Output = "try. no is there not, do or Do"</p>

<p>Here's what I've got so far:</p>

<pre><code>void ReverseString::reversalOperation(char str[]) {
    char* buffer;
    int stringReadPos, wordReadPos, writePos = 0;

    // Position of the last character is length -1
    stringReadPos = strlen(str) - 1;
    buffer = new char[stringReadPos+1];

    while (stringReadPos &gt;= 0) {
        if (str[stringReadPos] == ' ') {
            wordReadPos = stringReadPos + 1;
            buffer[writePos++] = str[stringReadPos--];
            while (str[wordReadPos] != ' ') {
                buffer[writePos] = str[wordReadPos];
                writePos++;
                wordReadPos++;
            }
        } else {
            stringReadPos--;
        }
    }

    cout &lt;&lt; str &lt;&lt; endl;
    cout &lt;&lt; buffer &lt;&lt; endl;
}
</code></pre>

<p>I was sure I was on the right track but all I get for an output is the very first word ("try.") I've been staring at this code so long I can't make any headway. Initially I was checking in the inner while look for a '/0' character as well but it didn't seem to like that so I took it out.</p>
