Question

In: Computer Science

** in C language please Write a function that remove a substring of str beginning at...

** 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) {

Solutions

Expert Solution

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.


Related Solutions

using java language only write a code Have the function StringChallenge(str) take the str string parameter...
using java language only write a code Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 ------- you have the following code edit it to get the result mport java.util.*; import java.io.*; class Main {   public static String StringChallenge(String str)...
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
************CODING IN C++ ONLY ******************** Instructions Write a function, remove, that takes three parameters: an array...
************CODING IN C++ ONLY ******************** Instructions Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted. Also, write a program to test the function. Your program should prompt the...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position. * If iPos is out-of-range, return (char*)NULL */ char* substring(char* str, int iPos); /* charPosition - return the position of the first occurance of c in str. * If c not present, return -1 */ int charPosition(char* str, char c);
Given a string str of even length, print out the first half.   Methods to use: substring(),...
Given a string str of even length, print out the first half.   Methods to use: substring(), length() Testing cases: WooHoo => Woo HelloThere   => Hello abcdef => abc ab   => a 0123456789   => 01234 kitten   => kit (Note that the sample input test cases are shown before the “=>” and the expected output each test case is after the “=>”. Your program should print out the expected output only, not the input.) this is for java language
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
In c++ language, write a void function GetYear that prompts for and inputs the year the...
In c++ language, write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT