1

I have program to remove the similar words from string but this program only removing at once word not a repeating words.

For example input:

sabunkerasmaskera kera

and should an output:

sabunmas

This my code:

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

void remove(char x[100], char y[100][100], char words[100]) {
    int i = 0, j = 0, k = 0;
    for (i = 0; x[i] != '\0'; i++) {
        if (x[i] == ' ') {
            y[k][j] = '\0';
            k++;
            j = 0;
        } else {
            y[k][j] = x[i];
            j++;
        }
    }
    y[k][j] = '\0';
 
    j = 0;
    for (i = 0; i < k + 1; i++) {
        if (strcmp(y[i], kata) == 0) {
            y[i][j] = '\0';
        }
    }
 
    j = 0;
    
    for (i = 0; i < k + 1; i++) {
        if (y[i][j] == '\0')
            continue;
        else
            printf("%s ", y[i]);
    }
    printf ("\n");
}

int main() {
    char x[100], y[100][100], kata[100];
    printf ("Enter word:\n");
    gets(x);
 
    printf("Enter word to remove:\n");
    gets(words);
    
    remove(x, y, words);
    
    return 0;
}

My program output its:

sabunkerasmaskerara

and that should not be the case. Maybe I need your opinion to fixed this program and also I need help to make it better.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
pratama
  • 45
  • 8
  • 1
    When you run this with a debugger, what is the *first* thing it does that you did not expect? – Scott Hunter Dec 08 '20 at 12:54
  • First of all ***never*** use `gets` in your programs. It's [so dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it even have been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Dec 08 '20 at 12:55
  • the program should an output sabunmas, but m program doesn't – pratama Dec 08 '20 at 12:56
  • Also, when you create a [mcve] to show us, please make sure it replicates or describes the problem you ask about *only*. If the shown code have other errors then it tend to distract from the actual problem. And if you claim you have a running program, then showing us code that won't even build is pretty bad. And why pass `y` as an argument, instead of defining it as a local variable inside the function? – Some programmer dude Dec 08 '20 at 12:57
  • sorry,I'll make a better question next time @Someprogrammerdude – pratama Dec 08 '20 at 12:57
  • As for your problem, all you really need are the [`strstr`](https://en.cppreference.com/w/c/string/byte/strstr), [`strlen`](https://en.cppreference.com/w/c/string/byte/strlen) and [`memcpy`](https://en.cppreference.com/w/c/string/byte/memcpy) functions, together with some knowledge about arrays, pointers and pointer arithmetic. – Some programmer dude Dec 08 '20 at 12:59
  • 1
    Given the input `sabunkerasmaskerara`, are you sure the desired output isn't `sabunsmasra` ? – Mark Benningfield Dec 08 '20 at 13:14
  • yes i try but not the desired output @MarkBenningfield – pratama Dec 08 '20 at 13:28

1 Answers1

0

Your solution does not work because it uses strcmp to compare the string portions, which only works if the substring is at the end of the string, as this makes it null-terminated.

You should instead use strstr to locate the matches and use memmove to shift the string contents.

There are other issues in your code:

  • do not use gets()
  • y is unnecessary for this task.
  • words is not defined

Here is a modified version:

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

char *remove_all(char *str, const char *word) {
    size_t len = strlen(word);

    if (len != 0) {
        char *p = str;
        while ((p = strstr(p, word)) != NULL) {
            memmove(p, p + len, strlen(p + len) + 1);
        }
    }
    return str;
}

int main() {
    char str[100], word[100];

    printf ("Enter string:\n");
    if (!fgets(str, sizeof str, stdin))
        return 1;
 
    printf("Enter word to remove:\n");
    if (!fgets(word, sizeof word, stdin))
        return 1;
    
    word[strcspn(word, "\n")] = '\0';  // strip the trailing newline if any

    remove_all(str, word);
    
    fputs(str, stdout);

    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189