Question

In c programming, I need to write a program that reverses each of the lines in...

In c programming, I need to write a program that reverses each of the lines in a file.

The program accepts two command line arguments:

  • The first one is the name of the input file
  • The second is the name of the output file

If the user didn't give two filenames, display an error message and exit.

The program reads in each line of the input file and writes each line in reverse to the output file.

Note, the lines should be in the same order, but each line is reversed, (words are written backwards).

Indicate how many characters there are in the file and how many lines if possible.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C code

==================================================================

#include<stdio.h>
#include<string.h>
#define MAXCHAR 1000

//reverse a string
void stringrev(char s[],int len)
{
int i=0;
int j=len-1;
char temp;
while(i < j) {
    temp = s[i];
    s[i++] = s[j];
    s[j--] = temp;
}
}


int main(int argc, char** argv)
{
   FILE *f1,*f2;
   char str[MAXCHAR];
   int line=0,count=0,k;

//check for 3 arguements
if(argc!=3)
{
printf("you have not given enough arguments\n");
return -1;
}

   // open file for reading
f1 = fopen(argv[1], "r");
//open file for writing
f2 = fopen(argv[2], "w");
//if input file exists
if (f1 != NULL ) {
  
    //printf("File open successfully!\n");
    while (fgets(str, MAXCHAR, f1) != NULL)
   {
         printf("%s", str);
       k=strlen(str);
   //count character
         count=count+k;
   //reverse string
        stringrev(str,k);
   //write to file
       fputs(str, f2);
       line++;    
   }
    printf("lines are: %d\ntotal character are: %d\n",line,count);
  
}
else {
    printf("ERROR:File not found.\n");
    return -1;
}


// close connection
fclose(f1);
fclose(f2);
  

return 0;
}

==================================================================

Output

Add a comment
Know the answer?
Add Answer to:
In c programming, I need to write a program that reverses each of the lines in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • Write a program in python that reads each line in a file, reverses its lines, and...

    Write a program in python that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt contain the lines: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go. and you run your Python file then output.txt contains The lamb was sure to go. And everywhere that Mary went Its fleece was white as snow Mary had...

  • ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of...

    ****C PROGRAMMING**** (100 points) Write a program that prompts the user to enter the name of a file. The program encoded sentences in the file and writes the encoded sentences to the output file. Enter the file name: messages.txt Output: encoded words are written to file: messages.txt.swt The program reads the content of the file and encodes a sentence by switching every alphabetical letter (lower case or upper case) with alphabetical position i, with the letter with alphabetical position 25...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

  • Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your...

    Write a C++ program that reverses a file. More specifically, given an input file (input.txt) your program will reverse every line in the input. This program does not have an output file, as it modifies the input file directly. You may assume that your input file has 1000 lines or less, if this helps. sample input: Hello World! I study C++ following output: !dlroW olleH ++C yduts I

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT