You are looking at: Practice test 2. Your place is still lesson 6, task 1: Arrays.
test/2/words.c: a line into words with pointers in the same array, the words reversed and the longest word in letters, not bytes.
Checklist
The file test/2/words.c, the program test2_words.
The program reads one line and splits it into words. A word is the characters between spaces; punctuation stays with the word (lyja,). There may be several spaces in a row, also at the start and the end of the line. Print:
The test sentences are Lithuanian on purpose: their letters Š, ė, ą, ž take 2 bytes each. To type such a sentence in the Run window, set CLion's console to UTF-8 first: Settings → Editor → General → Console → Default Encoding: UTF-8 (otherwise Š shows as Å ).
Sentence: Šiandien lyja, bet rytoj bus saulėta
Words: 6
Reversed: saulėta bus rytoj bet lyja, Šiandien
Longest: Šiandien, letters: 8
read_line("Sentence: ", line, sizeof line) – MAX_LINE is already defined.split_words(char *s, char *words[], int max) walks through s with a pointer, replaces spaces with '\0' and stores pointers to the starts of the words in words – like strtok, only your own. It returns the number of words. A 100-character line holds at most 50 words, so MAX_WORDS is 50.strlen("Šiandien") is 9, but there are 8 letters: in UTF-8 Š takes 2 bytes. The second and later bytes of one letter always look like 10xxxxxx, so a letter starts at every byte for which ((unsigned char)c & 0xC0) != 0x80. The function count_letters(const char *w) counts only those.assert, and main only calls them.ctest --preset debug -L test2
The first test is the example above. The second is ąžuolas berželis with spaces at the start, in the middle and at the end:
Words: 2
Reversed: berželis ąžuolas
Longest: berželis, letters: 8
Both words are 9 bytes, so with strlen ąžuolas would win, and the test would fail. While the file is empty:
=== test2_words_1: FAILED ===
The output differs on line 1.
expected: |Sentence: Šiandien lyja, bet rytoj bus saulėta|
got: (the output ended)
(\t = tab, ~ = a space at the end of the line)
When both output tests and test2_words_contracts pass, commit: Solve test 2: words.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.