Starting from:

$25

CPD-Exercise 1 Solved

Let’s write a program that can do word replacement in an article (e.g., replacing “he” with “she” in an article). The program will first ask the user to enter two strings. The first input indicates the string in the article that will be replaced and the second input represents the string that will be used to replace the first string. Furthermore, the user can choose whether to enter the parameter -i after the input of the two strings. Please see the table below for the meaning and usage of -i. Therefore, the input format would be like “he she” (replacing all he in the article with she) or “he she i” (replacing all he or He in the article with she). For example, the word “hello” in the article will become “shello”. 

Input
Output
 

Parameter
Condition
-i 
Change the compared condition from case sensitive to case insensitive. The default state is case-sensitive, which means that ‘a’ is different from ‘A’.
 

If the user enters the incorrect input format or the invalid parameter, the program

should show the correct input format as a reminder to users “The input format:  string1 string2 [parameter]” and terminate. After the user entered the

correct input format, the program will then ask the user to enter an article. Once the user finished entering the article and press enter (i.e., the new-line character), the program should replace every word (i.e., the first string and first appearence) in the article with the word represented by the second string and print those words that are replaced.  


The definition of a word is a string consisting of numbers, letters, and the dash character. The length of the input string will not exceed 100 characters, and the length of the article will not exceed 4095 characters.


The table below shows the example input and output. The underscored is the input from the user. For the efficiency of execution, you can use fprintf(stderr, “message you want to send”). You may also want to check these functions on the Internet when writing this exercise: fgets(), isalnum(), strstr().
 

Enter pattern, replacement, and at 

most one parameter: he it 

Enter the article: 

He is our new TA called Bo-Zheng, 

Chen. 

 It is ok not entering the parameters. 
Bo-Zitng Citn tit 
Enter pattern, replacement, and at most one parameter: he it -i 

Enter the article: 

He is our new TA called Bo-Zheng, 

Chen. 

 Cause the parameter “-i”, He has been replaced with it. 
it Bo-Zitng Citn tit it 
Enter pattern, replacement, and at most one parameter: he it -r 

Enter the article: 

 Example for Invalid case. 

 
The input format:  string1 string2 

[parameter] 
 

More products