user3819295
user3819295

Reputation: 861

Remove Duplicate words (only if followed) from char array

I am a little bit stuck and cant find out what is wrong here. I have an assignment to enter a sentence into char array and if there are duplicate and followed words(example : same same , diff diff. but not : same word same.) they should be removed. here is the function I wrote:

void Same(char arr[], char temp[]){
    int i = 0, j = 0, f = 0, *p, k = 0, counter = 0;
    for (i = 0; i < strlen(arr); i++){
        while (arr[i] != ' ' && i < strlen(arr)){
            temp[k] = arr[i];
            i++;
            k++;
            counter++;
        }
        temp[k] = '\0';
        k = 0;
        p = strstr((arr + i), (temp + j));
        if (p != NULL && (*p == arr[i])){
            for (f = 0; f < strlen(p); f++){
                *p = '*';
                p++;
            }
            f = 0;
        }
        j = counter;

    }
}

Upvotes: 1

Views: 1276

Answers (1)

Tony
Tony

Reputation: 1657

strtok is a handy function to grab the next word from a list (strsep is a better one, but is less likely to be available on your system). Using strtok, an approach like the following might work, at least for simple examples...

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

#define MAXPHRASELEN 1000
#define MAXTOKLEN 100

int main(int argc, char ** argv) 
{
    // Here is the sentence we are looking at
    char * tmp = "This is a test and and another test";
    // We will copy it to this variable
    char phrase[MAXPHRASELEN+1];
    strcpy(phrase, tmp);
    // And will put the altered text in this variable
    char new_phrase[MAXPHRASELEN+1];
    // This will be the last word we looked at
    char * lasttok = malloc(MAXTOKLEN+1);
    // This will be the current word
    char * tok = malloc(MAXTOKLEN+1);
    // Both words are initially empty
    new_phrase[0] = '\0';
    lasttok[0] = '\0';
    // Get the first word
    lasttok = strtok(phrase, " ");
    // If there is a word...
    if (lasttok != NULL) {
        // Put it in the altered text and add a space 
        strcat(new_phrase, lasttok);
        strcat(new_phrase, " ");
        // As long as there is a next word
        while ( (tok = strtok(NULL, " ")) != NULL ) {
            // See if it is the same as the last word
            if (strcmp(tok,lasttok) != 0) {
                // If it isn't, copy it to the altered text
                strcat(new_phrase, tok);
                // and add a space
                strcat(new_phrase, " ");
                // The current word becomes the last word
                lasttok = tok;
            }
        }
    }
    // Print the lot
    printf("%s\n", new_phrase);
}

If you really must write your own routine for grabbing the individual words, you could do worse than emulate strtok. It maintains a pointer to the beginning of current word in the string and puts a null character at the next separator (space character). When called again, it just moves the pointer to the character past the null, and puts another null after the next separator. Most string functions, when passed the pointer, will see the null as the end of the string and so just deal with the current word.

Minus comments, headers, and initialisation, it looks less threatening...

    lasttok = strtok(phrase, " ");
    if (lasttok != NULL) {
        strcat(new_phrase, lasttok);
        strcat(new_phrase, " ");
        while ( (tok = strtok(NULL, " ")) != NULL ) {
            if (strcmp(tok,lasttok) != 0) {
                strcat(new_phrase, tok);
                strcat(new_phrase, " ");
                lasttok = tok;
            }
        }
    }
    printf("%s\n", new_phrase);

Upvotes: 2

Related Questions