Write A C program called Reverse where it will read from an inputfile in .txt fo
ID: 3599781 • Letter: W
Question
Write A C program called Reverse where it will read from an inputfile in .txt form and write an output file in .txt form
The output file will contain the reversed word order of the input file.
The input file words are separated by one or more spaces ' ' or by horizontal tab ' '
The output file words should be separated by one space.
The input and output files, each line ends with new line character (‘ ’). Lines do not exceed 255 characters including new line character.
An example is shown below
Reverse input output
Input File
Output File
Beside an assault scenario.
When will a friendly cube?
The precedent.
This percent abides without an arm.
The recipe multiplies.
The provoking football.
scenario. assault an Beside
cube? friendly a will When
precedent. The
arm. an without abides percent This
multiplies. recipe The
football. provoking The
Input File
Output File
Beside an assault scenario.
When will a friendly cube?
The precedent.
This percent abides without an arm.
The recipe multiplies.
The provoking football.
scenario. assault an Beside
cube? friendly a will When
precedent. The
arm. an without abides percent This
multiplies. recipe The
football. provoking The
Explanation / Answer
create an input file input.txt and an output file output.txt(output file optional). Save the main file as main.c, compile in gcc and run. Give the below execution command and compilation command
Compilation command: gcc main.c
Execution command: ./a.out input
Open the output.txt file, you will get words in reverse order.
/**********************************/input.txt
Beside an assault scenario.
When will a friendly cube?
The precedent.
This percent abides without an arm.
The recipe multiplies.
The provoking football.
/***************************/
/*
============================================================================
Name : FileOp.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char* argv[]) {
//open input file
char str[20]={0};
strcpy(str,argv[1]);
strcat(str,".txt");
FILE *ip=fopen(str,"r");
if(ip==NULL)
{
printf("Input file does not exist ");
exit(-1);
}
//open or create output file
FILE *op;
if(argv[2]!=NULL) //checking if output file exist
{
strcpy(str,argv[2]);
strcat(str,".txt");
op=fopen(str,"w");
}
else
{
op=fopen("output.txt","w");
}
char buffer[255]={0};
while(fgets(buffer,sizeof(buffer),ip)) //read till end of input file
{
char wstr[255]={0};
char str[255]={0};
char *ptr=strtok(buffer," "); //tokenise read string
while(ptr)
{
strcpy(str,ptr); //copy the tocken content in temporary buffer
strcat(str," "); //pad space
strcat(str,wstr); //concatenate the previous buffer
strcpy(wstr,str); //update the whole buffer
ptr=strtok(NULL," ");
}
strcat(wstr," ");
fprintf(op,"%s",wstr); //print to output file
}
printf("Finished ");
fclose(ip); //close ip file
fclose(op); //close output file
return EXIT_SUCCESS;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.