Lesson 3.1 · Write it
Count until stop
A whole programPython or Java6 tests, 3 of them hidden
On a phone? Put the lines in order instead
Write a program that reads words one at a time and counts how many words were typed before the word stop.
- Read one word per line.
- When the word is exactly
stop, stop reading. - Print
Count:followed by the number of words that came beforestop. - The word
stopitself is not counted. - If the very first word is
stop, the count is0. - The comparison is case sensitive:
Stopis not the same asstop.
Your program is run several times. Only what it prints is checked.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
two words then stop (rules 3 and 4)
You type
- typed: hello
- typed: world
- typed: stop
It prints
Count: 2
stop immediately, count is 0 (rule 5)
You type
- typed: stop
It prints
Count: 0
one word then stop (rule 3)
You type
- typed: one
- typed: stop
It prints
Count: 1