You are to write a program that includes and tests a function called strreplace
ID: 3776078 • Letter: Y
Question
You are to write a program that includes and tests a function called strreplace (). The function strreplace () is to the designed such that it takes an input string from the user (or from a file) and replaces all occurrences of a user-specified word (old word) in this string with another user-specified word (new word). The new string replaces the old string following the function call. Note the following assumptions: Use the string input function gets() (or fgets() if you're using a file pointer) and output function puts () (or fputs ()) to read and write your strings. Assume that the input string, old word, and new word are entered separately. New word and old word can be different lengths and MUST be user inputs. Assume that the length of the input string never exceeds 250 characters. All user input and output MUST be done from main(). The description below specifies how your program must operate. You must use this method to receive credit for this exam. You must write and use the functions as described below: Include the usual (detailed) comment block including program name, author, date, inputs, outputs and description. To use the string functions, you will need to include the header file Create a function of type void to explain the program to the user with the description sent to Query the user for the input string in from or from a file (your choice). Ask the user to provide the old word and the new word. Call your function strreplace () from main (). Pass the input string, the old word, and the new word to this function. Your function should modify the input string "in-place" (i.e., the input string passed to the function is directly modified). Display the new string to stdout or to a file (your choice) from main(). As an example, for input string old word "the", new word "my", the output must read: For input string: the Lord of the Rings substituting "my" for "the" results in: my Lord of my Rings You must use at least the supplied four test sets (you can have your program execute once and loop over the data set or you may run your program each time for each data set). The four test strings are also in the file test_string. txt on BB.Explanation / Answer
#include<stdio.h>
#include<string.h>
void replaceSubstring(char [],char[],char[]);
main()
{
char string[250],sub[250],new[250];
printf("Enter a string: ");
gets(string);
printf("nter the substring: ");
gets(sub);
printf("Enter the new substring:");
gets(new);
strreplace(string,sub,new);
printf("resultant string after replacing……. %s ",string);
}
void strreplace(char string[],char sub[],char new[])
{
int stringLen,subLen,newLen;
int i=0,j,k;
int flag=0,start,end;
stringLen=strlen(string);
subLen=strlen(sub);
newLen=strlen(new);
for(i=0;i<stringLen;i++)
{
flag=0;
start=i;
for(j=0;string[i]==sub[j];j++,i++) /* Checks for the substring */
if(j==subLen-1)
flag=1; /* flag sets when substring is identified */
end=i;
if(flag==0)
i=i-j;
else
{
for(j=start;j<end;j++) /* Delete the substring */
{
for(k=start;k<stringLen;k++)
string[k]=string[k+1];
stringLen--;
i--;
}
for(j=start;j<start+newLen;j++) /* Inserting new substring */
{
for(k=stringLen;k>=j;k--)
string[k+1]=string[k];
string[j]=new[j-start];
stringLen++;
i++;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.