Night 17: Spell it out
Vous avez déjà terminé cette soirée, en Python ou en pseudocode. Elle compte pour les deux. Entraînez-vous à nouveau si vous le souhaitez.
Tonight: spell it out
- Your pet is learning to read. Tonight it looks at words letter by letter.
- You will learn one thing:
MID, to take characters out of a text.
Words for tonight
| Word | Meaning |
|---|---|
| character | one letter, number, space or sign |
| position | where something is: first, second, third … |
| backwards | from the end to the start |
Take one character with MID
MID(Text, Start, Length)gives part of a text.- Positions start at 1.
| Code | Gives |
|---|---|
MID("moon", 1, 1) |
"m" |
MID("moon", 4, 1) |
"n" |
MID("moon", 2, 2) |
"oo" |
Loop through text
- Count from 1 to
LENGTH(Word). Take one character each time.
DECLARE Word : STRING
DECLARE Index : INTEGER
Word ← "moon"
FOR Index ← 1 TO LENGTH(Word)
OUTPUT MID(Word, Index, 1)
NEXT Index
Count some letters
- A loop, an
IFand a counter find how many of one letter there are.
DECLARE Word : STRING
DECLARE Index, Count : INTEGER
Word ← "banana"
Count ← 0
FOR Index ← 1 TO LENGTH(Word)
IF MID(Word, Index, 1) = "n" THEN
Count ← Count + 1
ENDIF
NEXT Index
OUTPUT Count
Build new text
- Start with empty text
"". Add to it in the loop with&.
| Line in the loop | "star" gives |
|---|---|
Result ← Result & MID(Word, Index, 1) |
star (same order) |
Result ← MID(Word, Index, 1) & Result |
rats (backwards) |

- Now spell some words for your pet!
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Read a word. Use a FOR loop with MID to output each letter on its own line. kit gives k, i, t.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Read a word. Count how many times the letter a is in it, and output the count. banana gives 3.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
Read a word. Build it backwards in Result, one letter at a time, and output it. star gives rats.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
Read a word. Output it with every vowel (a, e, i, o, u) changed to *. hello gives h*ll*.
Cliquez sur Exécuter pour voir le résultat ici.