Brainlag
  1. C
  2. Core
16/38 lessons

Reading lines of input

30 min4 exercisesPremium

Read whole lines safely with fgets, strip the newline, parse them with sscanf and split them into words with strtok, the way real C tools do.

The skill
Read input a line at a time with fgets, then check and pick apart each line with sscanf and strtok.
You make
A score reader that skips broken lines, a command splitter for a text game and a playlist length counter.

Premium lesson

£7.99 a month or £59 a year. Cancel any time.

Builds on 15 The string library. Free to try: C Foundations and the playground.

Step 1 of 8
exercise_1.c

You've reached the good stuff.

4 exercises in this lesson, checked as you type. Premium opens every lesson after Foundations, in all four courses.

C, 32-bit: int and pointers are 4 bytes