4
\$\begingroup\$

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?

pacmaninbw
26.1k13 gold badges47 silver badges114 bronze badges
asked Oct 17, 2020 at 21:23
\$\endgroup\$
1
  • \$\begingroup\$ Maybe the loop body can be reduced to putchar( (isalnum(i) || i=='\n') ? i : delim); \$\endgroup\$ Commented Oct 22, 2020 at 9:18

1 Answer 1

3
\$\begingroup\$

It would be helpful for you to break up the code into functions, that will simplify each task.

Process the command line arguments first, then once you have the delimiter process the text files.

  1. Declare the variable delim above any logic and initialize it to the default value
  2. Process the command line to get the actual delimiter.
  3. Process the input file.

This should shorten the code because you only need one loop to process the input file rather than 2.

answered Oct 17, 2020 at 22:21
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.