Words in the message
Statement
A display board that shows text hangs at the entrance of a shopping arcade. The board shows a message one word at a time, turning over one slot for each word. The person in charge therefore has to know how many words a message holds before putting it up. The number of words is the number of slots the board has to turn over.
The message is given on one line. It is made of English letters and spaces only, and one space splits one word from the next. A space is sometimes attached at the very front or the very back of the message, and sometimes a blank line with no character at all comes in.
Given one line of message, write a program that prints the number of words it holds.
For example, suppose the message is jneyk llknud ifj imwpphbvbt. Cutting this message at the spaces gives the table below.
| Order | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Word | jneyk | llknud | ifj | imwpphbvbt |
There are four words, so the answer in this case is 4.
A space attached at the very front or the very back of the message does not count as a word. For example, think about messages whose only character is a, with a space in front of it or behind it. In every case, taking the spaces away leaves the single word a.
| Message | Number of words |
|---|---|
| a | 1 |
| One space and a | 1 |
| a and one space | 1 |
| One space and a and one space | 1 |
| A blank line with no character at all | 0 |
As in the last row of the table, a message with no character at all holds no word, so the answer is 0.
A message of up to 1,000,000 characters can come in on one line. It is therefore better to count with a single scan than to scan the message several times.
Input and Output Examples
Example 1
Input
jneyk llknud ifj imwpphbvbt
Output
4
Example 2
Input
The Curious Case of Benjamin Button
Output
6
Example 3
Input
a
Output
1
Example 4
Input
Output
0
Input
First, the first line gives the message to put up on the display board. The message is made of uppercase and lowercase English letters and spaces only, and its length is at most 1,000,000. One space splits one word from the next. A space may be attached at the very front and the very back of the message, and a blank line with no character at all may be given.
Output
Print the number of words the message holds, on one line. If there is no word at all, print 0. Do not attach a space after the value, and finish with one line break.
Requirements
- Read the input from standard input and write the result to standard output
- One space splits one word from the next
- A space attached at the very front or the very back does not count as a word
- If there is no word at all, print 0
- A message of up to 1,000,000 characters can come in, so counting with a single scan is better
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 4 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
4 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
The Curious Case of Benjamin Button
6
0
a
1
jneyk llknud ifj imwpphbvbt
4
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 4 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.