Programming contests

20 Programming Exercise for Beginners

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

Line Wrapping

Create a program that can be used to split the input lines after every 20th character. If a line is not longer than 20 characters, then it must appear in the output as it is.

Sample Input

  1. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  2. Aenean ultrices bibendum mollis.
download as text file

Output for Sample Input

  1. Lorem ipsum dolor si
  2. t amet, consectetur 
  3. adipiscing elit.
  4. Aenean ultrices bibe
  5. ndum mollis.
download as text file

Hints and Guide

This exercise does not really differ from a simple echo program. The only difference is that we need to print an extra newline after every twentieth character counted from the beginning of the line. It requires a single counter that must be reset to zero every time a newline character is read from the standard input or when the counter reaches 20.

One possible solution is available in: main.c.

Possible Improvements

Note that processing the input character by character can be very slow. A character buffer and the use of the fgets function can make the solution faster.

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