Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program must be written in C - whose input is an existing text fil

ID: 3598252 • Letter: T

Question

The following program must be written in C

- whose input is an existing text file with a name specified in a command line as the first argument,

- whose output is a new text file with a name specified in a command line as the second argument; requirements to the new text file are given below.

Input and output files should have extension ‘.txt’ but only names of the files are specified in a command line.

Example: suppose the executable version of your program is named p2.out. The program will be executed by a command line of the following form:

p2.out inputfile outputfile

In this case file inputfile.txt must exist, outputfile.txt will be created.

Requirements for the output text file:

The output file should have copy of each line of the input file in the same order of lines. Each line of the output file should consist of the same words as the corresponding line in the input file but ordered backward.

In the input file words are separated by one or more spaces (' ') or by horizontal tab (' '). In the output file words should be separated by single space.

In the input and output files, each line ends with new line character (‘ ’). Lines do not exceed 255 characters including new line character.

The outline of your C program is as follows.

1.Read command line arguments and check the correctness.

2.Create a new file named second argument + ".txt"

3.Proceed with converting input file lines into output file lines.

4.Stop and print out "Finished." once finish reading and writing the files.

Thus, each time your program is executed, it should handle just one input file.

Explanation / Answer

//Source code

#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

void print_upto_space(char *s, FILE *fp)
{
do {
if(*s == ' ')
break;
fprintf(fp,"%c",*s); // print the char of the word passed
} while (*s++ != ' ' );
}

void reverseWords(char *s, FILE *fp) // recursive function to reverse a word
{
char *p = strchr(s, ' '); // returns the pointer to the 1st appearance of ' '
while(p!= NULL && !isalpha(*(p+1))) // skip the extra spaces and tabs
p++;
if (p == NULL || *p==' ') {
fprintf(fp,"%s ", s); // print the word when no more space left
} else {
reverseWords(p + 1,fp);
print_upto_space(s,fp);
}
}

int main(int argc, char* argv[])
{
FILE *fp, *op;
char line[1000];
char rev_line[1000];
if(argc!=3) // taking command line argumnt and checking if valid number of arguments
{
printf("Invalid number of arguments ");
return -1;
}
fp = fopen(argv[1], "r");
if (fp == NULL)
exit(EXIT_FAILURE);
  
op = fopen(argv[2], "w");
if (op == NULL)
exit(EXIT_FAILURE);

  
while (fgets(line, sizeof(line), fp)) {
  
printf("%s", line);
line[strlen(line)-1] == ' ' ? line[strlen(line)-1] = '':1;
printf(" ");
reverseWords(line,op);
fprintf(op," ");

//printf("%s", line);

}

fclose(fp);
if (line)
free(line);
getchar();
return 0;
//exit(EXIT_SUCCESS);
}

// Sample output

//inputfile.txt contains

I am SME
SME of CSE
SME of HBASE

//Generated outputfile.txt contains

SME am I
CSE of SME
HBASE of SME