In: Computer Science
** in C language please
Write a function that remove a substring of str beginning at
index idx and length len.
// param str: the string being altered
// param idx: the starting index of the removed chunk
// param len: the number of characters to remove (length of
sub>
// removed) assumptions:
// 0 <= idx
// str is a valid string that terminates properly
//
// TODO: complete the function
void remove_substr(char str[], int idx, int len) {
code with comments in c (code to copy)
#include <stdio.h>
#include <stdlib.h>
// param str: the string being altered
// param idx: the starting index of the removed chunk
// param len: the number of characters to remove (length of sub>
// removed) assumptions:
// 0 <= idx
// str is a valid string that terminates properly
// TODO: complete the function
void remove_substr(char str[], int idx, int len) {
// get the starting index i and the ending index j of the substring to remove
int i=idx, j=idx+len;
//In a loop we will copy j to i until we reach the end of the string
while(str[j]!='\0'){
//copy character at j to i and increment i and j
str[i++]=str[j++];
}
//terminate the string
str[i]='\0';
}
//driver to test our function
int main()
{
char str[] = "Have a good and exciting day!";
remove_substr(str, 12, 13);
printf("remove_substr(\"Have a good and exciting day!\") = \"%s\"", str);
}
code screenshot in c
Sample Console Output Screenshot
Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.