Skip to content
ExamSpine

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.

  1. Read one word per line.
  2. When the word is exactly stop, stop reading.
  3. Print Count: followed by the number of words that came before stop.
  4. The word stop itself is not counted.
  5. If the very first word is stop, the count is 0.
  6. The comparison is case sensitive: Stop is not the same as stop.

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

    1. typed: hello
    2. typed: world
    3. typed: stop

    It prints

    Count: 2
  • stop immediately, count is 0 (rule 5)

    You type

    1. typed: stop

    It prints

    Count: 0
  • one word then stop (rule 3)

    You type

    1. typed: one
    2. typed: stop

    It prints

    Count: 1
solution.pySolution.java