Problem : You are to write a program that includes and tests a function called s
ID: 3775660 • Letter: P
Question
Problem :
You are to write a program that includes and tests a function called strreplace(). The function strreplace() is to be 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().
HINT: In your function strreplace (), define a temporary string and use the input string and the old and new words to build the new string. Once the new string is built, copy it into the old input string. These steps can easily be accomplished by applying the string functions strncpy(), strcpy(), strncat(), and strcat() (Note: strncpy() does NOT add the NULL). You may use strstr() to find each occurrence of the old word.
Detailed Description
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 <string.h>.
Create a function of type void to explain the program to the user with the description sent to stderr.
Query the user for the input string in main() from stdin 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 “the Lord of the Rings”, 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
Main String
Old word
New word
Result
“You say goodbye, I say hello.”
“say”
“wave”
“You wave goodbye, I wave hello.”
“Tiger got to hunt, bird got to fly; Man got to sit and wonder 'Why, why, why?'”
“got”
“like”
“Tiger like to hunt, bird like to fly; Man like to sit and wonder, 'Why, why, why?''”
“A horse is a horse, of course, of course.”
“horse”
“course”
“A course is a course, of course, of course.”
“Of all the gin joints in all the towns in all the world, she walks into mine.”
“the”
“10”
“Of all 10 gin joints in all 10 towns in all 10 world, she walks into mine.”
Can someone please help me with this question. I need the code in C language. Thank you :)
Main String
Old word
New word
Result
“You say goodbye, I say hello.”
“say”
“wave”
“You wave goodbye, I wave hello.”
“Tiger got to hunt, bird got to fly; Man got to sit and wonder 'Why, why, why?'”
“got”
“like”
“Tiger like to hunt, bird like to fly; Man like to sit and wonder, 'Why, why, why?''”
“A horse is a horse, of course, of course.”
“horse”
“course”
“A course is a course, of course, of course.”
“Of all the gin joints in all the towns in all the world, she walks into mine.”
“the”
“10”
“Of all 10 gin joints in all 10 towns in all 10 world, she walks into mine.”
Explanation / Answer
#include<stdio.h>
#include<string.h>
main()
{
char estring[250],oword[20],nword[20]; //estring is enter string and oword is old word and nword is new word variable
printf("Enter the string : ");
gets(estring);
printf("Enter the word to replace : ");
scanf("%s",&oword);
printf("Enter the new word to replace the old word : ");
scanf("%s",&nword);
strreplace(estring,oword,nword);
printf("%s",estring);
void strreplace(char estring[],char oword[],char nword[])
{
char t[50]; //taking temporary variable for storing the word
int estrleng=strlen(estring);
int owordleng=strlen(oword);
int nwordleng=strlen(nword);
for(int i=0;i<estrleng && estring[i]!=0;i++)
{
for(j=0;estring[i]!=' ' && estring[i]!=0;j++;i++)
{
t[j]=estring[i]; //seperate the words
}
if(strcmp(t,oword)==0)
{
int k=i-owordleng;
for(int m=0;m<owordleng;m++) //remove the old word
estring[j]=estring[j+1];
estringleng--;
i--;
}
for(m=0;m<nwordleng;m++) //inserting a new word
{
for(j=owordleng;j>=k;j--)
estring[j+1]=eword[j];
estring[k]=nword[m];
k++;
oword++;
i++;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.