1

So if A="aBcDeFg" and B="BDF", the output should be "aceg".

My idea was to check one-by-one if character of A is equal with any character of B (A[0] vs B[0]/B[1]...B[n] and so on). Therefore if they do not match, a counter variable is incremented. If the counter is smaller than length of B, then this character is deleted and when the counter equals length of B, the character is moved in an other string.

My version looks like this, but is not working:

void remove_characters(char s[], char r[])
{
    int k, i, j, l = 0;
    char s_copy[20];

    for (i = 0; s[i] != '\0'; i++){
        for (j = 0; r[j] != '\0'; j++)
            if (s[i] != r[j])
                k++;
        if (k = strlen(r)){
            s_copy[l++] = s[i];
            k = 0;
        }
    }
    puts(s_copy);
}

void main()
{
    char s1[20],s2[20];
    printf("Enter the first string: ");
    gets(s1);
    printf("Enter the second string: ");
    gets(s2);
    remove_characters(s1,s2);
}

Any ideas where is the problem?

  • 2
    `if (k = strlen(r))` is very likely a bug, an assignment doesn't make sense here – UnholySheep Jun 02 '21 at 21:17
  • Do basic debugging. Run your program in a debugger and trace it as it runs. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – kaylum Jun 02 '21 at 21:24
  • Please tell us more details about "is not working". Show a few examples of input, actual output and expected output. Is the order of the characters in `B` relevant? Would you expect the same result with e.g. `A="aDcBeFg"` and `B="BDF"` or `A="aBcBeBg"` and `B="BDF"`? Please [edit] your question to add requested information, don't use comments for this purpose. – Bodo Jun 02 '21 at 21:28
  • Unrelated but: 1. `gets` is terribly unsafe, deprecated for decades and should just never be used and 2. having *magic length* (**`20`** in char `s_copy[20];` is bad practice because it makes the function code hard to reuse. – Serge Ballesta Jun 02 '21 at 22:03
  • Your loops for `i` and `j` look like you're on the right path. What you're doing with `k`, though, looks wrong... It looks like it should be a boolean initialized just inside your `i` loop, set inside the `if` in the `j` loop, then checked as the condition in the `if` following the `j` loop rather than assigning the string length. – Perette Jun 02 '21 at 22:17

1 Answers1

0

Any ideas where is the problem?

There's more than one.

  • UnholySheep mentioned if (k = strlen(r)); you meant == rather than =.
  • k is not initialized before use; this is to be done before the inner loop.
  • s_copy is not null terminated.

Here's an alternative implementation which utilizes the standard function strchr and modifies s, which has the advantages that no limit is imposed on the length of s and that the result is available to the caller rather than just printed:

void remove_characters(char s[], char r[])
{
    char *t = s;
    do  if (*s && strchr(r, *s)) continue;
        else *t++ = *s;
      while (*s++);
}
Armali
  • 18,255
  • 14
  • 57
  • 171