2

I'm trying to write a function in C, that removes the second array's elements from the first array if the first array fully contains the second array in the exact same order.

I know it's complicated to ask in words, so here are some examples:

If Array-1 is hello and Array-2 is lo, then the output should be hel.

If Array-1 is hello hello and Array-2 is lo, then the output should be hel hel.

If Array-1 is hello and Array-2 is call (the first array does not contain all of the second array), then the output should be hello. (It shouldn't be changed.)

I wrote some code but when I try 2nd example (the hello hello one), it gives me hello hel, not hel hel.


char removeText (char a[], char b[], int lengthA, int lengthB) {

  int indexB = 0, i=0;
  char d[lengthA];

  for (; i<lengthA; i++) {

    if (a[i]==b[indexB]) {
      indexB++;
      if (indexB==lengthB) {
        for (int k=0, j=0; k<i-lengthB; j++, k++) {
          d[k] = a[j];
        }
      }
    }
    else {
      i -= indexB;
      indexB = 0;
    }
  }

  printf("%s", d);

  if(indexB!=lengthB) {
    return *a;
  }

  return *d;
}

int main(void) {

  char a[] = "hello hello";
  char b[] = "lo";
  int c = 11;
  int d = 2;
  
  removeText(a, b, c, d);

  return 0;
}

The output should be given with return. The printf("%s", d); part is just for trying if the code works or not.

I know what's wrong in my code. The

if (indexB==lengthB) {
    for (int k=0, j=0; k<i-lengthB; j++, k++) {
        d[k] = a[j];
    }
}

part causes the error but how can I fix it?

3
  • The return type char of the function does not make a sense. Commented Feb 18, 2021 at 14:20
  • If the first array is "banana" and the second is "ana"; would the result be "b" or "bna"? Commented Feb 18, 2021 at 14:50
  • strstr could help here... Commented Feb 18, 2021 at 14:51

1 Answer 1

1

Your problem is in the lines:

for (int k=0, j=0; k<i-lengthB; j++, k++) {
    d[k] = a[j];
}

Each time you find a pattern in a, you copy all a data into d, cancelling what have been done before.

To highlight this, you should modify your code with some debug info:

printf("copy to d from a, indexd for %d to %d\n", 0, i-lengthB)
for (int k=0, j=0; k<i-lengthB; j++, k++) {
    d[k] = a[j];
}

You could have noticed too that k and j are always equal.

One more question, how do you code if the pattern to remove is not at the end of input, or is not present in input ? Try it.


To solve your problem, I would advice a different approach: have a counter for ignored character in a and copy the character when pattern is not found, something like:

char * removeText (char a[], char b[], int lengthA, int lengthB) {

  int indexB = 0, i=0;
  int ignored = 0;
  char d[lengthA] = "";

  for (; i+ignored<lengthA; i++) {

    if (a[i+ignored]==b[indexB]) {
      indexB++;
      if (indexB==lengthB) {
        ignored += lengthB;
        printf("ignored = %d\n", ignored);
      }
    }
    else {
      i -= indexB;
      indexB = 0;
      printf("d[%d] = a[%d] (%c)\n", i, i+ignored , a[i+ignored ]);
      d[i] = a[i+ignored];
    }
  }
  printf("%s", d);
  strcpy(a, d);
  return a;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. It worked but there is one little problem. When I try just hello and lo, the output is hal�, but it should be just hal. What causes that strange character? The lengthA is 5 and lengthB is 2, as needed.
I think it's because the \0 is missing from d. One solution to fight this problem is to initialize d to "" (post edited)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.