This
C programming exercise will introduce you to character arrays
and the use of functions. The program will accept a series of lines
from stdin and keep track of the longest line that was entered
(the most characters in one line).
The provided source code has four unit tests that must pass for you to
receive full credit. However, passing these tests does not guarantee full credit.
Download the file
longest_with_tests.c
to a directory named LongestLine.
Complete the readline()
function and verify that you can correctly echo each line as it is
entered. An EOF (CTRL-D) terminates the program. You should make sure that the code truncates the input so that
no input line is over 80 characters long.
> gcc -g -o longest longest_with_tests.c
> ./longest
this is a long line <-- this is what you type (and hit 'enter')
this is a long line <-- this is what your code echoes
this is a longer line
this is a longer line
(CTRL-D)
Copy longest_with_tests.c to longest_with_tests2.c (use the cp command).
In this new file keep track of the longest line entered. Only print the
longest line entered after input is complete. You will need to
implement the copy()
function to save the longest line.
As soon as you think you have the copy function implemented compile your program
as shown and run the unit tests. Check the results carefully. All four tests must pass.
When all four pass then complete the rest of this part of the activity.
> gcc -g -o test longest_with_tests2.c
> ./test
Executing unit tests
Summary of unit tests:
4 tests passed
0 tests failed
> gcc -g -o longest longest_with_tests2.c
> ./longest
This is a long line.
This is a longer line.
This line has supercalifragilisticexpialidocious.
This is short.
(CTRL-D)
This line has supercalifragilisticexpialidocious.
To receive full credit for this activity you do the following: