Click here to Skip to main content
15,888,061 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.

How can I change my code?

What I have tried:

#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;
}
Posted
Updated 17-Oct-20 7:53am
Comments
Richard MacCutchan 17-Oct-20 7:59am    
It is not clear exactly what this code is supposed to do.
Rick York 17-Oct-20 12:15pm    
I have been in the business for a few decades and this is a new term for me. What is a "table processor"?

1 solution

Your delimiter comes in as third command argv and you should assign it to your char
C++
char delim = argv[2];
Use the debugger to see what your code is doing by watching some variables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900