Write a program that
reads several names from the standard input device and,
for each name, prints Hello, <Name>!
on the standard output device.
The standard input and output devices
are usually the keyboard and screen of a command line session.
$ ./hi
John
Hello, John!
Mary
Hello, Mary!
Smith
Hello, Smith!
Names are provided one per line, and do not contain spaces.
Names are composed of letters of the English alphabet or dashes (-
),
and have no more than 30 characters.
Input is terminated by the end-of-file (EOF).
John
Mary
Smith
Hello, John!
Hello, Mary!
Hello, Smith!
Submit your solution to be graded according to the following list:
-
)If you do not know where to start, read the Computer Science by Example book. First, setup your environment then learn the programming basics. Here are some hints:
Automated judge: Keep in mind that when your program is submitted it will not be run by a human but instead by an automated judge. Instructions should be followed exactly or the judge will not give you a full score.
Your program should not print messages like
Please type your name:
.
Instead, just print the required message for each name
followed by a line break
as in the example output.
Produce output as you go: You do not need to accumulate names and then produce everything at the end. It is enough to produce output as you go. As soon as you read a name, write the corresponding hello message to standard output.
Detecting the end of file. In this exercise, input is terminated by the end-of-file (EOF). Here are ways to detect EOF in C, Python and Haskell:
In C.
The scanf
function returns the numbers of elements read from stdin
.
Since this exercise requires you to read one number each line,
you can compare scanf
’s result to one as a while
condition:
while (scanf(...)==1) {
...
}
Which translates to, “while you’re able to read 1 item from standard input, do …”
In Python.
The pattern for line in sys.stdin:
can be used
to create a loop where a file is processed line by line
until the end-of-file (EOF).
In Haskell.
You can use interact
to declare the main function
and implement your solution as a function from String
to String
:
io :: String -> String
io = ...
main :: IO
main = interact io
EOF is then represented as the nil list constructor (""
or []
)
at the end of the argument String
.
On the terminal, you can simulate the end-of-file (EOF) by holding “Ctrl” and pressing “D”, i.e., Ctrl-D.
Redirecting input: On most systems (Windows / Linux / OS X), it is possible to redirect the standard input and output of your program to files, like so:
$ ./repeat <inputfile.txt >outputfile.txt
If you create a plain text file with the “example input”, the above command should produce a plain text file with the “example output”.
This is actually how the automated judge runs your program.
Easier exercises: If you have difficulty with this exercise, try the repeat and hi1 exercises first.
Copyright © 2020-2021 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/hi