Question: Please use Language C. Introduction A very common task that is often performed by programs that work with text files is the problem of locating

Please use Language C.

Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. Im sure weve all done this many times in working with Word, Notepad, or other editors. Since we dont have a GUI or other means of displaying the contents of a file all at once, lets modify the problem slightly. Rather than locating a specific substring within a file and then highlighting the results, as most modern programs do, lets write a C program that locates the occurrences of a specific substring within a file and then display the occurrence number as well as a portion of the text around the found substring. It should also count the number of occurrences and indicate that in a brief report at the end. Although it should work on any input file and any substring, well provide a copy of an input file to use for testing (i.e., Decl of Indep.txt). Overview

************************************************************************************************** Heres a high level overview of what your program should do: 1) Ask the user for the name of the input file. a) If the file is not present or cant be opened, display an error message and ask the user to enter another file name. 2) Ask the user for the substring to search for. 3) Calculate and display the found occurrences of the substring in the file. For each found occurrence, your program should display (a) the location number starting at 1 and going up to the total number of locations found, and (b) the portion of the string containing the found substring. This portion should consist of the substring and 8 characters before and 8 characters after the found substring. Sample Runs Here is a sample run looking for the string people in the file Decl of Indep.txt. Looking for the substring "people" in file "Decl of Indep.txt": Location 1: String: "for one people to" Location 2: String: "icts of people, unless" Location 3: String: "s those people would r" Location 4: String: " of the people." Location 5: String: " of our people."

CS1325 Introduction to Programming page: 4 There were 5 occurrences of the string "people" within the file "Decl of Indep.txt". Or consider another run looking for the substring oo in the file Decl of Indep.txt. Looking for the substring "oo" in file "Decl of Indep.txt": Location 1: String: "public goooood." Location 2: String: "ublic goooood." Location 3: String: "blic goooood." Location 4: String: "lic goooood." Location 5: String: "armed troops among" Location 6: String: ". They too have" Location 7: String: "of the good People" Location 8: String: "illiam Hooper" Location 9: String: "s Lightfoot Lee" Location 10: String: "Witherspoon" There were 10 occurrences of the string "oo" within the file "Decl of Indep.txt". Programming Notes: There are several points to be made about this problem in general, and about both of the example runs. 1) Do not use fscanf() to read the data from the input file. As you know, fscanf() tokenizes around white space and would therefore extract each word from the input file separately. Of course, this has both advantages and disadvantages. It would, for example, be a good function to use if we wanted to process only within individual words. But since our program should be able to detect substrings consisting of more than one word, fscanf() will not serve our purposes. Therefore, use fgets() as your primary input function. As we discussed in class, this will read a single line of input from the file at a time and place it in the target buffer. 2) Note that fgets() will put any newline character read from the input file into the target buffer. Note that since were talking about an input file, virtually every read line will have a newline character. This can cause problems if the substring is located at the end of a line since that newline will appear on the screen when the substring and its surrounding text are printed. a) Suggested solution: after you read a line of input, locate the position of the newline and overwrite it with a \0. The strchr() function would be very useful for this. CS1325 Introduction to Programming page: 5 3) Consider the first sample run (i.e., looking for the word people). Note that in locations 4 and 5, the word people appears not only at the end of a sentence but also at the end of a line of input. It is, therefore, impossible to display 8 characters after the found substring in those cases, since we are processing on a line-by-line basis. This is perfectly okay. If you follow the suggestion given in 2(a) above, this will not be a problem. 4) Consider the second sample run (i.e., looking for the substring oo). One instance of good in the input file has been deliberately modified to be goooood. It contains, therefore, 4 instances of oo instead of 1. Your program should be able to detect this. 5) There is more than one way to locate substrings within a larger string. One function that can do this is strncmp(), which compares n characters of one string with another. Another function is strstr(), which locates the position of one substring within another and returns a pointer to the found substring. 6) Build up your solution in a modular fashion, debugging as you go. Do not attempt to write the whole thing at once. If you feel lost at some point, simplify your problem down to something manageable. You might, for example, create a sample input file with a single word in it and see if your program can detect substrings within it. In any case, you should never have more than one function at once under development. Deliverables Please submit your C source code file and your pseudocode. There is no output file on this problem.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!