I need to write a C program that will implement the basic operations of table processors. The input will be text data read from a .txt file, operations will be defined using terminal arguments, and the output will be in a .txt file as well.
Program should be run the following way:
./main [-d delimiter] [name of the function for the table] <in.txt >out.txt
Where the -d
argument determines which symbols can be interpreted as separators of single cells, by default the delimiter
is a space character. Multiple cases of the same sign in the delimiter are ignored. The first sign in the delimiter character will be used as the separator of output values. name of the function
is the identifier of the function that will be called to perform certain tasks on the table. <in.txt
redirects reading from stdin to reading from in.txt, >out.txt
redirects outputting to stdout to outputting to out.txt
.
Here's what I wrote:
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
if((argc > 2) && (strcmp(argv[1], "-d") == 0)) {
char delim = *argv[2];
for (int i; (i = getchar()) != EOF; ) {
if(i == '\n')
putchar(i);
if(!((i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')) ){
putchar(delim);
continue;
}
putchar(i);
}
}
else if((argc == 2) && strcmp(argv[1], "-d") == 0) {
char delim = ' ';
for (int i; (i = getchar()) != EOF; ) {
if(i == '\n')
putchar(i);
if(!((i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z')) ){
putchar(delim);
continue;
}
putchar(i);
}
}
return 0;
}
The code works and does what it's supposed to, but I am not sure about the effectivity of the implementation. The requirements are: input table can't be an empty file, maximum length of a row (both input & output) is 10KiB, otherwise an error message should be shown. Using global variables isn't allowed, preprocessor macro #define
is. Functions for working with files and dynamic memory allocation aren't allowed as well.
What are the ways to change my code?