index | submit | rank | book

age – Name and Age

Write a program that reads a series of strings and integers and prints name and age message.

John is 23 and Jane is 32

Input and Output

Input will consist of a several lines each containing a string n and a natural number a where

|n| ≤ 30, i.e.: the string n is no more than 30 characters

0 ≤ a ≤ 180

For each line of input, your program should produce a line containing the message:

<n> is <a> years old.

with <n> replaced by n and <a> replaced by a.

The number a may be given with leading zeroes on the input and should be given without leading zeroes on the output.

Input is terminated by the end-of-file (EOF).

Example input

John 23
Jane 32

Example output

John is 23 years old.
Jane is 32 years old.



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:

  1. 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: or Please type your age:. Instead, just print the corresponding message followed by a line break as in the example output.

  2. Produce output as you go: You do not need to accumulate input and then produce everything at the end. It is enough to produce output as you go. As soon as you read a name and a number, write the corresponding message to standard output.

  3. 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 items read from stdin. Since this exercise requires you to read a string and a number each line, you can compare scanf’s result to two as a while condition:

        while (scanf(...)==2) {

      Which translates to, “while you’re able to read 2 items 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.

  4. Beware of leading zeroes. C users should beware of leading zeroes. Use %d instead of %i to avoid treating numbers with leading zeroes as octals. Python and Haskell users should convert the “age” to an integer variable to automatically get rid of leading zeroes.

  5. There is no need to check boundaries: It is out of scope of this exercise to check boundaries. The boundaries for a are given for information only: 0 ≤ a ≤ 180. This just means that to get a full score, it is enough to write a program that works under these conditions, e.g.: a “C-int” field will be enough to hold the age value. There’s no need to check that the age a is in this interval.

try first: repeat hi age1

try also: swap

try next: triple inc owes

index | submit | rank | book

Copyright © 2020-2023 Rudy Matela
All rights reserved