Programming contests

20 Programming Exercise for Beginners

January 1, 2019 12:00 AM – December 31, 2019 12:00 AM

Input Validation

Create a program that validates its input. The input is valid if it is true for all the input lines that the line consists of decimal digits only or contains nothing else just lowercase letters.

Input Specification

The input consists of ASCII characters. The length of the input data and the number of input lines are not restricted.

Sample Input

  1. 123456789
  2. abcdabcd
  3. asasas
  4. 123456
download as text file

Output Specification

The output consists of a line terminated by a newline character. The line must contains the word true if the input is valid or the word false otherwise.

Output for Sample Input

  1. true
download as text file

Hints and Guide

We recommend to read the input character by character. Three boolean variable should managed in case when other than a newline character is read from the input:

  1. a variable which is set to true (1) if a digit is read,
  2. a variable which is set to true (1) if a lowercase letter is read,
  3. a variable which is set to true (1) if any other character is read.

Any time when a newline character is read from the input, all the variables must be reset to false (0).

Note that we should use integers to store boolean values, hence there is no dedicated Boolean type in the C programming language.

After processing the next character on the input, the program can terminate immediately, recognizing that the input is invalid, if the first two or the third variable is set to true. If there is no more character on the input, then the input is valid.

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019