working #3 for kiri

This commit is contained in:
2025-10-23 20:06:09 +07:00
parent e5e1c10b24
commit cc842e52c6
10 changed files with 301 additions and 550 deletions

View File

@@ -1,44 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE 4096
int replace_char(char *buf, int key, int *replacements_left);
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <in> <out> <cap>\n", argv[0]);
return 1;
}
FILE *fin = fopen(argv[1], "r");
if (!fin) { perror("fopen input"); return 1; }
FILE *fout = fopen(argv[2], "w");
if (!fout) { perror("fopen output"); fclose(fin); return 1; }
int cap = atoi(argv[3]);
if (cap < 0) {
fprintf(stderr, "invalid cap\n");
fclose(fin);
fclose(fout);
return 1;
}
int total = 0;
char line[MAX_LINE];
while (fgets(line, sizeof(line), fin)) {
if (cap > 0) {
// key is unused, but pass the first byte for symmetry
int key = (unsigned char)line[0];
int repl_line = replace_char(line, key, &cap);
total += repl_line;
}
fputs(line, fout);
}
fclose(fin);
fclose(fout);
printf("total_replacements: %d\n", total);
return 0;
}