Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a C function that removes a portion of a string?

Tags:

c

string

I want to write a C function that removes a portion of a string for a given index range.

For example, if the input string is "ABCDEFGHIJK" and the start index is 2 and the end index is 5 then the output should be: "ABGHIJK".

I am attempting to do this using two functions, one function that gets the substring we want to delete:

void get_substring(char string[], char substring[], int start, int end) {
    strncpy(substring, string + start, end - start + 1);
}

and then a second function that deletes this substring:

void remove_portion(char string[], char substring[]) {
    // memmove?
}

Another possibility 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 correct approach? Are there any built in functions from string.h that can be useful here?

like image 590
user6005857 Avatar asked Oct 06 '16 08:10

user6005857


People also ask

How do you delete a particular part of a string?

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove.

Is there a remove function in string?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

Can you write a method that will erase any character from a string?

There is no method to replace or remove last character from string, but we can do it using string substring method.

How do I remove a value from a string?

One can use string slice and slice the string before the pos i, and slice after the pos i. Then using string concatenation of both, ith character can appear to be deleted from the string.


3 Answers

I would go with using memmove as in the 2nd approach:

void remove_portion(char string[], int start, int end) 
{
    if (start>=0 && end>=start && start<strlen(string) && end<strlen(string)) {  // some more sanity checking (EDIT added later)
         memmove(string+start, string+end+1, strlen(string)-(end+1)+1);  // final +1 to copy string terminator
    }
}

Also note that in your first example (with strncpy) is not going to to copy the ending string terminator \0 to substring. So you'll need to add

substring[end - start + 1]= '\0'; 

to that.

like image 154
Anachronism Avatar answered Nov 14 '22 21:11

Anachronism


Use this :

void remove_portion( char * str, int start, int end){
char* stro = calloc(strlen(str - (end-start+1)), sizeof(char));
strncpy(stro,str,start);
strcat(stro,&str[end]);
strcpy(str,stro);
}

Add conditions also.

like image 22
Sashank Avatar answered Nov 14 '22 22:11

Sashank


New to C myself, but this worked for me:

void remove_portion(char str[], int start, int end) {
    assert((end > start) && (strlen(str) > end));
    char out[strlen(str) - (end - start)];
    int i, j = 0;
    for (i = 0; str[i] != '\0'; i++) {
        if ((i < start) || (i > end))
            out[j++] = str[i];
    }
    out[j] = '\0';
    strcpy(str, out);
}
like image 27
Aidenhjj Avatar answered Nov 14 '22 22:11

Aidenhjj