-
Notifications
You must be signed in to change notification settings - Fork 12
/
replace.c
63 lines (51 loc) · 2.14 KB
/
replace.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*--------------------------------------------------------------------*/
/* replace.c */
/* Author: ??? */
/*--------------------------------------------------------------------*/
#include "str.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
/*--------------------------------------------------------------------*/
/* If pcFrom is the empty string, then write string pcLine to stdout
and return 0. Otherwise write string pcLine to stdout with each
distinct occurrence of string pcFrom replaced with string pcTo,
and return a count of how many replacements were made. Make no
assumptions about the maximum number of replacements or the
maximum number of characters in strings pcLine, pcFrom, or pcTo. */
static size_t replaceAndWrite(const char *pcLine,
const char *pcFrom, const char *pcTo)
{
/* Insert your code here. */
}
/*--------------------------------------------------------------------*/
/* If argc is unequal to 3, then write an error message to stderr and
return EXIT_FAILURE. Otherwise...
If argv[1] is the empty string, then write each line of stdin to
stdout, write a message to stderr indicating that 0 replacements
were made, and return 0. Otherwise...
Write each line of stdin to stdout with each distinct occurrence of
argv[1] replaced with argv[2], write a message to stderr indicating
how many replacements were made, and return 0.
Assume that no line of stdin consists of more than MAX_LINE_SIZE-1
characters. */
int main(int argc, char *argv[])
{
enum {MAX_LINE_SIZE = 4096};
enum {PROPER_ARG_COUNT = 3};
char acLine[MAX_LINE_SIZE];
char *pcFrom;
char *pcTo;
size_t uReplaceCount = 0;
if (argc != PROPER_ARG_COUNT)
{
fprintf(stderr, "usage: %s fromstring tostring\n", argv[0]);
return EXIT_FAILURE;
}
pcFrom = argv[1];
pcTo = argv[2];
while (fgets(acLine, MAX_LINE_SIZE, stdin) != NULL)
/* Insert your code here. */
fprintf(stderr, "%lu replacements\n", (unsigned long)uReplaceCount);
return 0;
}