/* Include lib */
#include <stdio.h>
#include <assert.h>
#include <string.h>

/* Define */
#define SENTENCE_MAX_WORD       64
#define SENTENCE_MAX_CHAR       (SENTENCE_MAX_WORD * 5)

char sentence[SENTENCE_MAX_CHAR] = " apple pear  plum apple plucot";
const char splitter[2] = " ";

/* API */
/* API cut sentence to words */
#if 0
int sentence_split(char *sentence, char **argv, int *argc)
{
    char *token;

    *argc = 0;
    token = strtok(sentence, splitter);
    while (token != NULL) {
        argv[*argc] = token;
        *argc = *argc + 1;
        //printf("%s\n", token);
        token = strtok(NULL, splitter);
    }
    
    return 0;
}
#else
int sentence_split(char *sentence, char **argv, int *argc)
{
    int i, found = 0;

    if (!sentence || !argc || !argv) {
        printf("NULL input !!\n");
        return -1;
    }

    *argc = 0;
    for (i = 0; sentence[i] != 0; i++) {
        if (sentence[i] == splitter[0]) {
            sentence[i] = 0;
            found = 0;
            //printf("%s\n", argv[*argc - 1]);
        } else if (found == 0) {
            argv[*argc] = &sentence[i];
            *argc = *argc + 1;
            
            /* Check overflow */
            if (*argc > SENTENCE_MAX_WORD) {
                printf("Sentence is over max words %d\n", SENTENCE_MAX_WORD);
                return -1;
            }

            found = 1;
        }
    }

    return 0;
}
#endif

/* API delete word */

/* API search repeat */
int delete_repeat_word(char *sentence)
{
    char *argv[SENTENCE_MAX_WORD];
    char output[SENTENCE_MAX_CHAR];
    int i, j, argc, pos;
    
    if (!sentence) {
        printf("NULL input !!\n");
        return -1;
    }

        
    /* Cut sentence */
    sentence_split(sentence, argv, &argc);
//    printf("argc %d\n", argc);
//    for (i = 0; i < argc; i++) {
//        printf("%s\n", argv[i]);
//    }
    
    /* Search repeat to store only first appearance words */
    /*
     * Algo: with each word location: if it is not appeared above, put it to output.
     * If it is appeared before, it means this word already put to output -> ignore it, go to next.
     */
    pos = 0;
    for (i = 0; i < argc; i++) {
        //printf("\n%s\n", argv[i]);
        for (j = 0; j < i; j++) {
            if (strncmp(argv[i], argv[j], strlen(argv[i])) == 0) 
                break;
        }
        
        if (j >= i) {
            memcpy(&output[pos], argv[i], strlen(argv[i]));
            pos += strlen(argv[i]);
            memcpy(&output[pos], &splitter[0], 1);
            pos += 1;
        }
    }

    /* Copy to original again */
    memset(sentence, 0, SENTENCE_MAX_CHAR);
    memcpy(sentence, output, SENTENCE_MAX_CHAR);

    return 0;
}

/* Main func interract with user */
int main(void)
{

    printf("Input : %s\n", sentence);

    /*
     * FIXME: the way in this code is cut sentence by " ", so it may remove
     * double " " in output. Means not original " " in original sentence.
     * However it may still match with request because double " " is also repeat of " ".
     */
    if (delete_repeat_word(sentence) == 0)
        printf("Output: %s\n", sentence);
    else
        return -1;
    
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: