The following program takes an input as an existing text file with a name specif
ID: 3598639 • Letter: T
Question
The following program takes an input as an existing text file with a name specified in a command line as the first argument, then gives an output as a new text file with a name specified in a command line as the second argument. The output file must also:
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.
In the following code, it is made so the words in the output file are separated by a single space, and each line ends with a new line character. However, I need help making it so each line of the output file consists of the same words as the corresponding line in the input file, but ordered backwards.
Here is the code to be worked on:
#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 *ptr=strtok(buffer," "); //tokenise read string
while(ptr)
{
strcat(wstr,ptr);
strcat(wstr," "); //pad space
ptr=strtok(NULL," ");
}
fprintf(op,"%s",wstr); //print to output file
}
printf("Finished ");
fclose(ip); //close ip file
fclose(op); //close output file
return EXIT_SUCCESS;
}
This is written in C
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int readline(FILE *f, char *buffer, size_t len)//For reading line by line from input file
{
char c; int i;
memset(buffer, 0, len);
for (i = 0; i < len; i++)
{
int c = fgetc(f);
if (!feof(f))
{
if (c == ' ')
buffer[i] = 0;
else if (c == ' ')
{
buffer[i] = 0;
return i+1;
}
else if(c=='.')
{
buffer[i]=0;
return i+1;
}
else
buffer[i] = c;
}
else
{
return -1;
}
}
return -1;
}
void reverse(char *begin, char *end);
//Function definition to reverse the words
void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s;
while( *temp )
{
temp++;
if (*temp == '')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */
reverse(s, temp-1);
}
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
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(readline(file,buffer,255)!=-1)
{
//printf("%s",buff);
char wstr[255]={0};
char *ptr=strtok(buffer," "); //tokenise read string
while(ptr)
{
strcat(wstr,ptr);
strcat(wstr," "); //pad space
ptr=strtok(NULL," ");
}
reverseWords(wstr);//reverses order of word in string "wstr"
fprintf(op,"%s ", wstr);
}
fclose(file);
printf("Finished ");
fclose(ip); //close ip file
fclose(op); //close output file
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.