I want to write a C function that removes part of a string for a given range of indices.
For example, if the input line is "ABCDEFGHIJK" and the start index is 2 and the end index is 5, then the output should be: "ABGHIJK".
I am trying to do this using two functions, one function that gets the substring we want to remove:
void get_substring(char string[], char substring[], int start, int end) { strncpy(substring, string + start, end - start + 1); }
and then a second function that removes this substring:
void remove_portion(char string[], char substring[]) {
Another possibility that I was thinking about is to directly modify the original string without using a substring:
void remove_portion(char string[], int start, int end) { // if end is less then the length of the string, then // copy everything after string[end] into a temp string // Then replace string[start] with '\0' and then concatenate // string and temp. // If end is greater than the length of string then just replace // string[start] with '\0'. }
Is this the right approach? Are there any built-in functions from string.h that may be useful here?
source share