Programming contests

20 Programming Exercise for Beginners

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

Unified MAC Addresses

Create a program that transforms MAC addresses to a unified format.

A MAC address consist of 6 bytes in hexadecimal form. Two consecutive bytes are separated by a single character: a hyphen or a colon.

The program should process all input lines. If the line contains a MAC address, then it must be transformed to a form where the hexadecimal digits do not contain lowercase letters and all separators are replaced by colons. The input line should not be accepted as a valid MAC address if it is not exactly 17 characters long or if both separator characters occur in it.

Input Specification

The input is a sequence of lines containing arbitrary strings without any further restrictions. Each input line is terminated by a newline character.

Sample Input

  1. 10-20-30-40-5a-6B
download as text file

Output Specification

For each line of the input that contains a valid MAC address, print one line to the output containing the MAC address in the required format. Each line must be terminated by a newline character.

Output for Sample Input

  1. 10:20:30:40:5A:6B
download as text file

Hints and Guide

While processing the input, we can notice that only those lines can contain a MAC address which consist of 17 characters (excluding the newline character). So it is enough to store the first 17 characters of each input line. If a line is longer than 17 characters, than we have nothing to do with it.

To make the processing easier, it is a good strategy to convert each letter from ‘a’ to ‘f’ to uppercase format. After that, 2 patterns can be accepted as a MAC address: one where we use hyphens and another one where we use colons to separate the bytes. Please note that mixing those separators in an input line is not accepted; that is why we need two different patterns. A pattern consists of 17 strings which contain the acceptable characters at the corresponding position on the input line.

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