forked from COS217/Strings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace.c
More file actions
105 lines (84 loc) · 3.79 KB
/
Copy pathreplace.c
File metadata and controls
105 lines (84 loc) · 3.79 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*--------------------------------------------------------------------*/
/* replace.c */
/* Author: Anish Kataria */
/*--------------------------------------------------------------------*/
#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)
{
/* Variable declarations at the beginning of the block */
size_t replacementCounter = 0;
const char *currentPosition = pcLine;
const char *foundPosition;
/* Ensure that the input pointers are valid */
assert(pcLine != NULL);
assert(pcFrom != NULL);
assert(pcTo != NULL);
/* Check if the 'from' string is empty */
if (Str_getLength(pcFrom) == 0) {
printf("%s", pcLine);
return 0;
}
/* Iterate through the line to find and replace occurrences */
while ((foundPosition = Str_search(currentPosition, pcFrom)) != NULL) {
/* Calculate the length of the segment before the match */
size_t segmentLength = (size_t)(foundPosition - currentPosition);
/* Print the segment before the matched 'from' string */
printf("%.*s", (int)segmentLength, currentPosition);
/* Print the 'to' string as a replacement */
printf("%s", pcTo);
/* Move the current position past the matched 'from' string */
currentPosition = foundPosition + Str_getLength(pcFrom);
/* Increment the replacement counter */
replacementCounter++;
}
/* Print any remaining part of the line after the last match */
printf("%s", currentPosition);
return replacementCounter;
}
/*--------------------------------------------------------------------*/
/* 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 { REQUIRED_ARG_COUNT = 3 };
char inputLine[MAX_LINE_SIZE];
char *searchString;
char *replaceString;
size_t totalReplacements = 0;
/* Validate the number of command-line arguments */
if (argc != REQUIRED_ARG_COUNT) {
fprintf(stderr, "Usage: %s <from_string> <to_string>\n", argv[0]);
return EXIT_FAILURE;
}
/* Assign the 'from' and 'to' strings from command-line arguments */
searchString = argv[1];
replaceString = argv[2];
/* Process each line from standard input */
while (fgets(inputLine, MAX_LINE_SIZE, stdin) != NULL) {
/* Perform replacement and accumulate the count */
totalReplacements += replaceAndWrite(inputLine, searchString, replaceString);
}
/* Output the total number of replacements to stderr */
fprintf(stderr, "%lu replacements\n", (unsigned long)totalReplacements);
return 0;
}