Emptypeace
Emptypeace

Reputation: 139

C++ Reverse the order of words in a string with pointers

This is my attempt at taking an input and then produce an output with the words in the reverse order. Note: I can only use pointers.

    Input: What is your name? 
    Output: name? your is What

I have looked at the other solutions posted here and thought tried to implement them as much as possible but I keep running into problems. Currently it only displays the first letter of the line. I would appreciate any help to fix it!

#include <iostream>
using namespace std;

#include <cstring>

int main( )
{
    char input[255] = {' '};
    char *head, *tail;
    char temp; 
    int i = 0; 

    cin >> input;

    head = input;
    tail = input;

    while(*tail!='\0')
    {
        tail++;
    }

    while (tail <= head){
        temp = *head;
        *head=*tail;
        *tail=temp;

        *head++;
        *tail--;
    }

    cout << input;
    cout << endl;

    return 0;
}

Upvotes: 0

Views: 3542

Answers (3)

jensa
jensa

Reputation: 2890

Low-level:

void reverseStr(const char* str, char* szBuffer)
{
    int j = strlen(str) - 1, i = j, k = 0;
    if(j < 0)
        return;
    while(j >= 0){
        if(j-1 < 0 || *(str+j-1) == ' '){
            strncpy(szBuffer+k, str+j, i-j+1);
            szBuffer[k+i-j+1] = ' ';
            k += i-j+2;
            i = j-2;
            j -= 2;
        }
        else{
            --j;
        }
    }
    szBuffer[k-1] = '\0';
}

int main(int argc, char** argv)
{
    const char* str = "this is a string?";

    char szBuffer[256];
    reverseStr(str, szBuffer);

    std::cout << szBuffer;

}

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409206

How about something like this pseudo-codeish:

read_input_into_string();
trim_leading_and_trailing_whitespace();
convert_multiple_whitespace_in_string_to_single_space();

for (char* current_string_ptr = find_end_of_string();
     current_string_ptr > start_of_input;
     current_string_ptr--)
{
    if (*current_string_ptr == ' ')
    {
        // +1 because `current_string_ptr` is at the space,
        // and we want to print the word without leading spaces
        std::cout << (current_string_ptr + 1) << ' ';

        // Terminate string at the space
        *current_string_ptr = '\0';
    }
}

It's not complete (i.e. won't print the first word in the input), but I leave that as an exercise for the reader. :)

Upvotes: 1

Richard Dong
Richard Dong

Reputation: 704

Try this. Without anything referring STL.

#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char **argv)
{
    char input[255];
    char *head; // head of a word.
    char *tail; // tail of a word.
    cin.getline(input, 255); // read a whole line.
    tail = input + strlen(input) - 1; // if you can use <cstring>?
    while (tail >= input) {
        if (*tail == 0 || *tail == ' ') {
            tail--; // move to the tail of a word.
        } else {
            tail[1] = 0;
            head = tail;
            while (head >= input) {
                if (head == input || *(head - 1) == ' ') {
                    cout << head << " "; // output a word.
                    tail = head - 1; // seek the next word.
                    break;
                }
                head--; // move to the head of a word.
            }
        }
    }
    cout << endl;
    return 0;
} 

Upvotes: 3

Related Questions