Information
For example, if you are given a text file named poem.txt that contains these 4 lines of text:enum FILE_ERR replacestr(const char *filename, const char *oldstr, const char *newstr);
and you are to replace each occurrence of the string are with the string may be, this is how you would call the function:Roses are red. Violets are blue. Some poems rhyme. But not this one.
and this is what would be printed out on the screen:enum FILE_ERR result = replacestr("poem.txt", "are", "may be");
Here is a header file that you should use (replacestr.h):Roses may be red. Violets may be blue. Some poems rhyme. But not this one.
/* Max length of a line */
#define LINE_MAX 2048
/* Possible file errors */
enum FILE_ERR {feNONE, feINPUT};
enum FILE_ERR replacestr(const char *filename, const char *oldstr, const char *newstr);
Here is a driver file: (main.c, HTML
Text) You must specify the filename, old string, and new string on the
command line like this:
replacestr poem.txt are "may be"
If you don't provide 3 arguments to the program, it will display this message:
Usage: replacestr filename oldstring newstring where: filename is the file to search through oldstring is the text to search for newstring is the text to replace oldstring with Example: poem.txt are "may be" If either of the strings contain spaces, it must be surrounded by quotes.
The name of your implementation file should be replacestr.c and the command to compile it will look like this:
Here is a skeleton for replacestr.c to get you started:gcc -O -Werror -Wall -Wextra -ansi -pedantic main.c replacestr.c -o replacestr
#include <stdio.h>
#include <string.h>
#include "replacestr.h"
static void print_replace(const char *string,
const char *oldstr,
const char *newstr)
{
}
enum FILE_ERR replacestr(const char *filename,
const char *oldstr,
const char *newstr)
{
}
Here are some sample outputs using the input file: input1-LF.txt. Windows version is here: input1-CRLF.txt
./replacestr input1-LF.txt one ONE > output1.txt
./replacestr input1-LF.txt one Wubalubadubdub! > output2.txt
./replacestr input1-LF.txt "one one one one one one one one" XXX > output3.txt
./replacestr input1-LF.txt "one one" XXX > output4.txt
Here's another test using input2-LF.txt. Windows version: input2-CRLF.txt using this command:
./replacestr input2-LF.txt the THE
Output: output5.txt
Another test:
Output: output6.txt./replacestr input2-LF.txt " " "" > output6.txt
A bigger test using doi-100-2-LF.txt and the Windows version: doi-100-2-CRLF.txt
Output: output7.txt./replacestr doi-100-2-LF.txt "of" "X X X" > output7.txt
A stress test using stress-LF.txt and the Windows version: stress-CRLF.txt
Output: output8.txt./replacestr stress-LF.txt one X > output8.txt
With Windows, use the CRLF version of the input files on the command lines and replace ./replacestr with replacestr.
All of the files in one zip: all-in-out-files.zipApproximate number of lines of code: 30.
Notes
static void print_replace(const char *string, const char *oldstr, const char *newstr);
The output should look like this:Red monkeys play in the big red tent with three blue elephants that turned red
Notice that the strings are case-sensitive, e.g. UPPERCASE is not the same as lowercase.Red monkeys play in the big yellow tent with three blue elephants that turned yellow
We are going to assume that, if you can open the file, then you can read all of the lines. It is possible to get a read error during the process, but you don't have to handle that at this point.