Skip to content

interactive · runs entirely in your browser

Stage-by-stage visualiser

Edit the input below. The TypeScript port of the original C program reruns on every keystroke, and each stage shows what changed, what it printed, and the C that did it.

Coffee run: A small group chat that exercises every stage, including a line merge in stage 5.

stdin

6 messages · 8 emoticons · 16 lines

The gutter shows how each line is used:msgmessage###separatordicdictionarystopends dictionary·not readlong!too long

Messages first (first line ≤ 50 bytes in this program, others ≤ 280), then a line with ###, then up to 50 dictionary lines such as :),happy.

stage 1 of 5 · stage_one() + count_tokens()

Read the first message

Read the first line and count its tokens by counting commas.

first message · commas numbered

First message: Coffee,before,class?,:-),:-). It contains 4 commas.

count_tokens()

4 commas + 1 = 5

bytes read28 / 50

The task allows 280 characters, but stage_one passes MAX_EMTCN_LENGTH (50) to read_one_msg.

start of the program

stdout · stage 1

Stage 1
==========
Number of tokens: 5

Under the hood

Words and emoticons are separated by commas, so a line with k commas has k + 1 tokens. One left-to-right scan is enough; nothing needs to be stored.

The skeleton's read_one_msg pulls characters with getchar until a newline, carriage return or end of input, and aborts if the line is longer than the limit it is given.

time
O(n)
extra space
O(1)

n is the length of the first message. count_tokens always walks a fixed 50-byte window of the buffer.

stdout

$ ./program < input.txt

exit 0

Byte-for-byte what the compiled C program prints for this input. Click a block to jump to its stage.