Question: Write a c++ program in that file to perform a Search and Replace All operation. This operation consists of performing a case-sensitive search for all
Write a c++ program in that file to perform a Search and Replace All operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they dont read the command line information in the course document titled Using the Compilers IDE. Your program:
1. must check the success/failure status of opening any file immediately after opening it;
2. may assume, for simplicity, that it will only be used with text files;
3. may assume, for simplicity, that the input and output file names will be different;
4. may assume, for simplicity, that no character sequence will be split across multiple lines;
5. must support multiple occurrences of a character sequence on a single line;
6. must support cases where the length of the search character sequence is different from the length of the replacement character sequence;
7. must get the following information from the command line (spaces must be allowed in all items): a. the name of the file to search; b. the name of the file to store the results in; c. the sequence of characters to search for; d. the sequence of characters to use as replacements;
8. must verify that exactly the correct number of command arguments are present and exit with an error message and error code if not;
9. must not access any command arguments before verifying that they actually exist;
10. must not use data types list, queue, stack, string, or vector.
11. must not call the strlen more than once for the same string, including placing such a call inside a loop. I recommend reading the file one line at a time and parsing it with the standard strstr library function to find each search sequence, then using the following 4-step algorithm to process each sequence:
1. Copy from the old file into the new file up to where a search sequence starts;
2. Copy the substitution sequence into the new file;
3. Skip the search sequence in the old file;
4. Repeat steps 1-3 until EOF is reached. Manually re-run your program several times, testing at least the following 2 cases with instructor-supplied data file TestFile1.txt.
Choose any name you wish for the output file as long as it is different from the input file name:
For example (Test #1), if the input file contained, These are the answers to neither of their questions! the output file would contain, These are Who is John Galt? answers to neiWho is John Galt?r of Who is John Galt?ir questions!
Note that there were three replacements on one line and that the was part of another word in two of those cases. Hints: EOF cannot be correctly stored or detected using data type char or unsigned char; use type int instead 6 (Notes 4.3A & 4.3B).
Since EOF only occurs when a read (or write) is attempted, testing for it before such an attempt is meaningless and wrong. Only test after such an attempt before the value obtained from reading is actually used (Note 4.3B).
Below is a description of the step-by-step algorithm I used to implement the Search and Replace All requirements of this exercise. I make no claim that this is the best approach and you may feel free to either use it or implement your own. Regardless of what you implement, make sure you understand exactly what it does. Drawing a diagram of memory as the algorithm progresses always helps. The basic concept of the algorithm is that for each line in the input file, do the following:
1. Set a pointer to the first character in the input line.
2. Copy characters into the output file from the pointer to where the string to be replaced starts.
3. Write the replacement string into the output file.
4. Move the pointer to the next character in the input line after the string to be replaced.
5. Repeat steps 2 through 4 until the end of the line has been reached. These are the implementation specifics for the algorithm above:
1. Do all the standard things, including declaring needed variables and opening the input and output files.
2. Implement a loop statement that contains the following 3 statements (a, b, and c) in order: a. a statement that gets the next line from the input file and stores it into a character buffer. (The newline character must be discarded.)
If the EOF condition occurs, terminate the loop. Otherwise, proceed to step b below. b. a for statement that does everything in steps i and ii below (in order): i. does the following in its control section. The control section is the portion of the for statement that is in parentheses just after the keyword for:
1) The initial expression initializes a character pointer (Ill call it cp1) to point to the beginning of the character buffer you are reading each line into.
2) The controlling expression assigns the return value of the strstr function to a different character pointer (Ill call it cp2). The first argument of strstr will be cp1 and the second argument will be a pointer to the first character of the string you are looking for in the file.
3) The loop expression is empty. ii. does the following in the loop body:
1) Uses the write function to write to the output file. The first argument of write will be cp1 and the second will be cp2-cp1.
2) Writes the replacement string to the output file.
3) Updates cp1 by assigning to it the sum of cp2 and the length of the string 46 you are searching for in the file. c. a statement that writes the string in cp1 and a newline character to the output file. SAVE BELOW AS:
FileText1.txt The number-sign or "stringizing" operator (#) converts macro parameters (after expansion) to string constants. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal. The string literal then replaces each occurrence of a combination of the stringizing operator and formal parameter within the macro definition. White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.
Manually re-run your program several times, testing at least the following 2 cases with instructor-supplied data file TestFile1.txt, which must be placed in the programs working directory. For example (Test #1), if the input file contained, These are the answers to neither of their questions! the output file would contain, These are John Galt? answers to neiJohn Galt?r of John Galt?ir questions! Note that there were three replacements on one line and that the was part of another word in two of those cases.
Hints: The value of EOF cannot be correctly stored or detected using data type char or unsigned char; use type int instead (Notes 4.3A & 4.3B). Since EOF only occurs when a read (or write) is attempted, testing for it before such an attempt is meaningless and inappropriate. Only test after such an attempt before the value obtained from reading is actually used (Note 4.3B). Not testing files for a successful opening is bad programming (Notes 10.3 & 10.4B). It is not necessary to determine the length of the replacement string. You may use any algorithm you wish to complete this exercise as long as none of the requirements/restrictions are violated. The following describes the algorithm I used and recommend. Drawing a diagram of memory as the algorithm progresses always helps: General Algorithm Description: For each line in the input file { Set a pointer to first character in line; For each search sequence found in line using the strstr library function { Copy characters into the output file from the pointer to where the search sequence starts; Write the replacement string into the output file; Move the pointer to the next character in the input line after the search sequence ends; } Copy remainder of line into the output file; } Algorithm Step-by-Step Implementation Details: 1. Do all the standard things, including declaring needed variables and opening the input and output files. 2. Implement a loop statement that contains the following 3 statements (a, b, and c) in order: a. a statement that gets the next line from the input file and stores it into a character buffer. (The newline character must be discarded.) If the EOF condition occurs, terminate the loop. Otherwise, proceed to step b below. b. a for statement that does everything in steps i and ii below (in order): i. does the following in its control section. The control section is the portion of the for statement that is in parentheses just after the keyword for: 1) The initial expression (Note 3.5) initializes a character pointer (Ill call it cp1) to point to the beginning of the character buffer you are reading each line into. 43 2) The controlling expression (Note 3.5) assigns the return value of the 44 strstr function to a different character pointer (Ill call it cp2). The first 45 argument of strstr will be cp1 and the second argument will be a 46 pointer to the first character of the string you are looking for in the file. 47 3) The loop expression (Note 3.5) is empty. 48 ii. does the following in the loop body: 49 1) Uses the write function to write to the output file. The first argument of 50 write will be cp1 and the second will be cp2-cp1. 51 2) Uses the << operator to write the replacement string to the output file. 52 3) Updates cp1 by assigning to it the sum of cp2 and the length of the string 53 you are searching for in the file. 54 c. a statement that writes the string in cp1 and a newline character to the output file.
Code is enough than diagram
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
