help with c Please do not copy from the other's answer, and it's not right Your friend's monitor is too small and can only display up to 50 characters on a line before they run off the screen. They aren't good with computers, so you decide to write a program for them called truncator.c that will reprint input so that no line is longer than 50 characters. Specifically, for lines less than or equal to 50 characters, simply reprint the line. For lines greater than 50 characters, insert a newline at each 50-character interval. Note: If the 51st character of a line in the input is a newline '\n', then you should not print it (to avoid unnatural double newlines in the text). Depending on your solution, you may not need extra code for this. So worry about this case last. Tip: Write your own file with test input for the case where the 51st character is a newline. In the bottom-right corner of Vim, you can see the line and column number of your cursor and plan your 51-character input accordingly. Furthermore, to give developers feedback on their programs for 50-character-displays, you decide to add additional functionality to your program by displaying the line numbers of all lines over 50 characters long and the total number of these offending lines.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 19PE
icon
Related questions
Question

help with c

Please do not copy from the other's answer, and it's not right

Your friend's monitor is too small and can only display up to 50 characters on a line before they run off the screen. They aren't good with computers, so you decide to write a program for them called truncator.c that will reprint input so that no line is longer than 50 characters. Specifically, for lines less than or equal to 50 characters, simply reprint the line. For lines greater than 50 characters, insert a newline at each 50-character interval.

Note: If the 51st character of a line in the input is a newline '\n', then you should not print it (to avoid unnatural double newlines in the text). Depending on your solution, you may not need extra code for this. So worry about this case last.

Tip: Write your own file with test input for the case where the 51st character is a newline. In the bottom-right corner of Vim, you can see the line and column number of your cursor and plan your 51-character input accordingly.

Furthermore, to give developers feedback on their programs for 50-character-displays, you decide to add additional functionality to your program by displaying the line numbers of all lines over 50 characters long and the total number of these offending lines.

For example:

learncli$ cat test0.txt This line is under 50 characters long This line is just over 50 characters long, you see? This line is soooooooooooooooooo looooouoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong! learncli$ cat test0.txt | ./a.out This line is under 50 characters long This line is just over 50 characters long, you see ? This line is soooooooooooooooooo looooouoooooooooo oooooooooooooooooooooooooooooooooooooooooooooooooo oooooong! Total lines over 50 chars: 2 Offending lines: 1, 2, learncli$

Some additional things to note:

  • The first line is the 0th line.
  • You can assume that none of the test input will contain over 100 illegal lines, so use an array of length 100 to store the line numbers in.
  • There should be a newline \n after the reformatted output and a newline after the line that says "Offending lines..."*
  • The test input will not contain any tabs, which generally take up as much space as multiple characters.
  • Windows uses carriage return, so if a new file is created in Windows, for each new line character, there will be a carriage return ('\r') before it. This can be avoided by doing all work inside of the container, or using the dos2unix command (after installing it with apt install dos2unix), or ignoring that character.

Tips for testing

  • Some test inputs and results are included for you to check for correctness. You can also write your own tests.

  • You can test for the correctness of your programs by running:

    learncli$ gcc <progam-name.c> learncli$ cat test0.txt | ./a.out > myresult learncli$ diff myresult test0-result.txt

    The first command compiles the c program to a.out. The second pipes the text in test0.txt to the standard input of your program and redirects the output of your program to a new file called myresult (instead of just printing it to the screen). And the third command runs the diff program on myresult and test0-result.txt, which will show any differences between the output from your program and the correct output. If nothing prints after running the diff command, then your output matches!

 

test0.txt

This line is under 50 characters long
This line is just over 50 characters long, you see?
This line is soooooooooooooooooo looooouoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong!

test0_result.txt

This line is under 50 characters long
This line is just over 50 characters long, you see
?
This line is soooooooooooooooooo looooouoooooooooo
oooooooooooooooooooooooooooooooooooooooooooooooooo
oooooong!

Total lines over 50 chars: 2
Offending lines: 1, 2,

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr