top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Reverse the order of the words

+6 votes
1,057 views

Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it

posted Oct 29, 2013 by Mona Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Just for the confirmation, u want if the array is "I love QueryHome" then the output should be "QueryHome love I" and space is the word separator.

3 Answers

+2 votes

Read one character at a time and put it in a buffer array until space is encountered in original string and then put null character at the end(to make it string) in the buffer array. Now put this buffered string in a stack. Repeat until end of original string reached. Now pop stack and copy popped string to original string one by one till the stack is not empty.

answer Oct 30, 2013 by Aditya
But this is a very costly method to achieve, using an extra buffer and stack..
Reverse the words in a string without changing their order?
Yes expensive in terms of space but very clean and understandable.
Another approach what I could think is first reverse the whole string and then reverse the order of characters in every word.

For example if initial string is "some junk string" the reversed string will be "gnirts knuj emos".
In the second step reverse letters of every word till spaces. Final string will become "string junk some";
+1 vote

See if this helps

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LENGTH 1000
#define MAX_WORD_LENGTH 80

void rev(char *str) 
{
    size_t str_length = strlen(str);
    int str_idx;
    char word_buffer[MAX_WORD_LENGTH] = {0};
    int word_buffer_idx = 0;

    for (str_idx = str_length - 1; str_idx >= 0; str_idx--)
        word_buffer[word_buffer_idx++] = str[str_idx];

    memcpy(str, word_buffer, word_buffer_idx);
    str[word_buffer_idx] = '\0';
}

int main(int argc, char **argv) 
{
    char *line = NULL;
    size_t line_length;
    int line_idx;
    char word_buffer[MAX_WORD_LENGTH] = {0};
    int word_buffer_idx;

    /* set up line buffer - we cast the result of malloc() because we're using C++ */

    line = (char *) malloc (MAX_LINE_LENGTH + 1);
    if (!line) {
        fprintf(stderr, "ERROR: Could not allocate space for line buffer!\n");
        return EXIT_FAILURE;
    }

    /* read in a line of characters from standard input */

    getline(&line, &line_length, stdin);

    /* replace newline with NUL character to correctly terminate 'line' */

    for (line_idx = 0; line_idx < (int) line_length; line_idx++) {
        if (line[line_idx] == '\n') {
            line[line_idx] = '\0';
            line_length = line_idx; 
            break;
        }
    }

    /* put the reverse of a word into a buffer, else print the reverse of the word buffer if we encounter a space */

    for (line_idx = line_length - 1, word_buffer_idx = 0; line_idx >= -1; line_idx--) {
        if (line_idx == -1) 
            word_buffer[word_buffer_idx] = '\0', rev(word_buffer), fprintf(stdout, "%s\n", word_buffer);
        else if (line[line_idx] == ' ')
            word_buffer[word_buffer_idx] = '\0', rev(word_buffer), fprintf(stdout, "%s ", word_buffer), word_buffer_idx = 0;
        else
            word_buffer[word_buffer_idx++] = line[line_idx];
    }

    /* cleanup memory, to avoid leaks */

    free(line);

    return EXIT_SUCCESS;
}
answer Oct 30, 2013 by Majula Joshi
0 votes

YOUR PROBLEM ANSWER IS THIS TRY

 #include <stdio.h>
    void reverse();

    void main() {      
        printf("Please enter a sentence: ");
        reverse();
        getch();
    }    

    void reverse() {    
        char c;
        scanf("%c",&c);
        if(c != '\n') {      
            reverse();
            printf("%c",c);
    }   

}
answer Jul 4, 2014 by Gaurav Maurya
But this is only a trick by which u can see the reverse of what you are entering, you can not reuse the code in the program.
Similar Questions
+6 votes

Say you are given "Hello World"
Output should be "olleH dlroW"

+6 votes

Write a program to reverse the bits of an unsigned integer ??

+4 votes
List => 1 --->  2 --->  3 --->  4 ---> 5 ---> 6
                                      /        \
                                    10          7
                                     \          /
                                      9 <---  8


OURTPUT => 1 --->  2 --->  3 --->  4 ---> 10 ---> 9
                                          /        \
                                        5           8
                                         \          /
                                          6 <---  7
+3 votes

List => 1 2 3 4 5 6 7 8 9

N=3 Out Put => 1 2 3 6 5 4 7 8 9
N=4 Out Put => 1 2 6 5 4 3 7 8 9
N=5 Out Put => 1 2 7 6 5 4 3 8 9

0 votes

Suppose you are given "Luv you 123 luv me" the expected output is "vuL uoy 123 vul em"

...