0

I have a structure in which I have a flat array that store the list of strings and the offset will track the starting position where the string is added in the array.

typedef struct
{
   char element[256];
   int offset;
} A;

void A_append(A* a, const char *str) {
    // Concatenate on the end of element.
    strcat(&a->element[a->offset], str);

    // Increment the offset to the new end.
    a->offset += strlen(str);
}
int main() {
    A a = { .element = "" };
    a.offset = 0;

    char string1[] = "one";
    A_append(&a, string1);

    char string2[] = "two";
    A_append(&a, string2);
}

Now I want to search the string "two" in the flat array and then delete the string.

Please let me know if this feasible. If yes then how?

Ken White
  • 123,280
  • 14
  • 225
  • 444
sbg123
  • 23
  • 4

1 Answers1

0

You can use the char* strstr(const char* string, const char* substring) function to find a substring within a string, e.g. strstr(&a->element, "two") which will return a pointer to the first occurrence of the substring within the string provided. There is no builtin C function to remove the substring automatically, but an example implementation using memmove is given here.

zmckevitt
  • 13
  • 4