// The first example:
char text[] = "henri";
char *p;
p = text;
*(p + 1) = 'E'; // Output = hEnri
// Now If we want to remove the "e" ie hnri, we would go for?????
*(p + 1)=?????
The obvious answer is to copy the rest of the array “back” one position. But this seems… unpleasant. Surely there is some better way?
Copying is the best solution and really the only solution when you’re removing elements from an array. (You can special case the element at the end of the array, but that’s it.) I don’t understand why it’s so terrible.
You need to do one of two things. Both involve some copying.
1) Copy the ‘n’ to where the ‘E’ is, the ‘r’ to where the ‘n’ is, the ‘i’ to where the ‘r’ is, and then null terminate it.
2) Copy the ‘h’ to where the ‘E’ is and then always use the pointer to the ‘h’s new location. This may not always be an option.