108 lines
2.5 KiB
C
108 lines
2.5 KiB
C
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <stdbool.h>
|
|
#define INPUT_FILE "./input.txt"
|
|
#define OUTPUT_FILE "./testo.txt"
|
|
|
|
void writeChar(FILE *fout, int * charInLine, int * written, char chr, bool isOrig) {
|
|
if (chr != '\n') {
|
|
fputc(chr, fout);
|
|
} else {
|
|
for (int i = *charInLine; i <= 24; i++) {
|
|
fputc(' ', fout);
|
|
}
|
|
}
|
|
|
|
if (*charInLine >= 24 || chr == '\n') {
|
|
fprintf(fout, "| c:%d \n", *written + 1);
|
|
*charInLine = 0;
|
|
*written = 0;
|
|
} else {
|
|
if (isOrig) {
|
|
*written += 1;
|
|
}
|
|
*charInLine += 1;
|
|
}
|
|
}
|
|
|
|
void elabora(FILE *fin, FILE *fout) {
|
|
char lastChar, currChar, buf;
|
|
int count, i;
|
|
|
|
int charInLine = 0, written = 0;
|
|
bool requiresSpace = false;
|
|
bool requiresCaps = false;
|
|
|
|
while (!feof(fin))
|
|
{
|
|
buf = fgetc(fin);
|
|
|
|
if (feof(fin))
|
|
break;
|
|
|
|
currChar = buf; // This is done to avoid havind EOF char in currChar.
|
|
|
|
if (requiresSpace) {
|
|
if (currChar != ' ') {
|
|
writeChar(fout, &charInLine, &written, ' ', false);
|
|
}
|
|
|
|
requiresSpace = false;
|
|
}
|
|
|
|
if (requiresCaps && isalpha(currChar)) {
|
|
if (currChar >= 97 && currChar <= 122) {
|
|
currChar -= 32;
|
|
}
|
|
|
|
requiresCaps = false;
|
|
}
|
|
|
|
if (isdigit(currChar)) {
|
|
writeChar(fout, &charInLine, &written, '*', true);
|
|
} else {
|
|
writeChar(fout, &charInLine, &written, currChar, true);
|
|
}
|
|
|
|
if (currChar == '.' || currChar == ',' ||currChar == ';' ||currChar == ':' ||currChar == '!' ||currChar == '?' ||currChar == '\''||currChar == '('||currChar == ')') {
|
|
requiresSpace = true;
|
|
}
|
|
|
|
if (currChar == '.' ||currChar == '!' ||currChar == '?') {
|
|
requiresCaps = true;
|
|
}
|
|
}
|
|
|
|
// Add '\n' at the end of the file if not present
|
|
if (currChar != '\n') {
|
|
writeChar(fout, &charInLine, &written, '\n', false);
|
|
}
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
|
|
FILE *fp_read, *fp_write;
|
|
|
|
fp_read = fopen(INPUT_FILE, "r");
|
|
fp_write = fopen(OUTPUT_FILE, "w");
|
|
|
|
if (fp_read == NULL)
|
|
{
|
|
printf("Error opening file\n");
|
|
return 1;
|
|
}
|
|
if (fp_write == NULL)
|
|
{
|
|
printf("Error opening file\n");
|
|
return 2;
|
|
}
|
|
|
|
elabora(fp_read, fp_write);
|
|
|
|
fclose(fp_read);
|
|
fclose(fp_write);
|
|
|
|
return 0;
|
|
} |