Question

In: Computer Science

please answer with coding from The second edition C programming language textbook /* Changes all occurrences...

please answer with coding from The second edition C programming language textbook

/* Changes all occurrences of t in s to u, result stored in v

*

* Example:

*

* char v[100];

* replace("hello", "el", "abc", v) => v becomes "habclo"

* replace("", "el", "abc", v) => v becomes "" (no change)

* replace("hello", "abc", "def", v) => v becomes "hello" (no change)

* replace("utilities", "ti", "def", v) => v becomes "udeflidefes"

*

*/

void replace(char *s, char *t, char *u, char *v)

{

}

Solutions

Expert Solution

void replace(char *s, char *t, char *u, char *v)
{
    int flag,len=strlen(t);
    for(int i=0;s[i]!='\0';i++)
    {
        
        if (strncmp(s+i,t,len) == 0)
        {
            strcat(v,u);
            i=i+len-1;
            continue;
        }
        strncat(v,s+i,1);
            
    }
    printf("%s\n",v); //you can omit this line if you don't need it,I did it to print the result
}

Here is the full code , if you need

#include<stdio.h>
#include<string.h>


void replace(char *s, char *t, char *u, char *v)
{
    int flag,len=strlen(t);
    for(int i=0;s[i]!='\0';i++)
    {
        
        if (strncmp(s+i,t,len) == 0)
        {
            strcat(v,u);
            i=i+len-1;
            continue;
        }
        strncat(v,s+i,1);
            
    }
    printf("%s\n",v);
}


int main()
{
    char s[]="utilities";
    char t[]="ti";
    char u[]="def";
    char v[100]={0};
    replace(s,t,u,v);
    return 0;
}

Related Solutions

Please answer the problem below in C programming language: Create a pointer activity source file -...
Please answer the problem below in C programming language: Create a pointer activity source file - cptr2.c - that takes two arguments, a number from 1 to 3, and a string sentence(s). Create variables for a character, an integer, a string pointer. Based on integer value you will use that number of string pointers. The string variable is a string pointer that has not been allocated.    Define pointers to those variables types without any initialization of those points to the...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right?...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right? There are 5 objectives and 20 points each. Please indicate in code (or comments) where each objective begins and ends. It may be prudent to place each objective in it's own function. In this lab you will be reading in a file of student results. The students each ran 3 laps in a race and their times to complete each lap are posted in...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Book - Introduction to Programming Using Visual Basic 11th Edition by David I. Schneider Programming Language...
Book - Introduction to Programming Using Visual Basic 11th Edition by David I. Schneider Programming Language - Visual Studio 2017 RESTAURANT MENU Write a program to place an order from the restaurant menu in Table 4.13. Use the form in Fig. 4.70, and write the program so that each group box is invisible and becomes visible only when its corresponding check box is checked. After the button is clicked, the cost of the meal should be calculated. (NOTE: The Checked...
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for...
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for homework first... Thank you!!! Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three...
In C programming Generalize the to binary() function of Listing 9.8 (from your textbook) to a...
In C programming Generalize the to binary() function of Listing 9.8 (from your textbook) to a to base n(number, base) function that takes a second argument in the range 2–10. It then should convert (and print) the number that is its first argument to the number base given by the second argument. For example, to base n(129,8) would display 201, the base-8 equivalent of 129. Test the function in a complete program.
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following:...
Instructions: 1. Please use only C as the language of programming. 2. Please submit the following: (1) the client and the server source files each (2) a brief Readme le that shows the usage of the program. 3. Please appropriately comment your program and name all the identifiers suitable, to enable enhanced readability of the code. Problem: Write an ftp client and an ftp server such that the client sends a request to ftp server for downloading a file. The...
Programming language is Java In this second assignment, you will calculate currency conversions from United States...
Programming language is Java In this second assignment, you will calculate currency conversions from United States Dollars (USD) to Indian Rupees (INR) or to Euros (EUR) or to British Pounds (GB) using methods and formatting the results. Use these ratios: 1 USD to 72.282250 INR 1 USD to 0.913465 EUR 1 USD to 0.833335 GBP Your task is to write a program that • displays a menu to choose conversion from dollars to rupees, euros, or pounds. • displays the...
Apply four concepts in psychology textbook second edition to medicine as a career and give specific...
Apply four concepts in psychology textbook second edition to medicine as a career and give specific examples of how you will use each concept in the career (medicine). Hint: choose four concepts, for instance chapter 6-memory, you chose a concept like recognition and you give examples of how the knowledge of recognition can be used in my career as a medical doctor
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT