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

#define S_SPACE 0
#define S_CHAR 1

void substr(char buf[], char *str, int wstart, int wend) {
    strncpy(buf, str + wstart, wend - wstart + 1);
    buf[wend - wstart + 1] = '\0';
}

main()
{
    char *s = "It was a pleasant evening.  A boy and a girl sat in the park.";
    int i, j, state, newstate;
    char *caption;
    char status[80], wordbuf[80];
    int wordstart, wordend, wordlen;

    i = strlen(s);
    state = isspace(s[i]) ? S_SPACE : S_CHAR;
    --i;
    wordend = i;

    while (i >= 0) {
        newstate = isspace(s[i]) ? S_SPACE : S_CHAR;
        if (newstate != state) {
            if (newstate == S_SPACE) {
                substr(wordbuf, s, i + 1, wordend);
                printf("%s ", wordbuf);
            }
            else {
                wordend = i;
            }
            state = newstate;
        }
        else {
        }
        --i;
    }

    // Dump the remains if still in char state
    if (state == S_CHAR) {
        substr(wordbuf, s, 0, wordend);
        printf("%s\n", wordbuf);
    }

    return 0;
}
