Stop Word Printer

Stop Word Printer

Medium Python Control Flow 10 views
Explanation Complexity

Problem Statement

Words are provided one by one. Output all words until you see the word stop (case sensitive). Do not output stop itself.

Input Format

• Words are provided one by one (each word on a new line)

• Input ends when the word "stop" is read

Output Format

• Print each word on a new line

• Do not print "stop"

Example

hello
world
python
stop
hello
world
python

Constraints

• Each word length ≤ 10^5

• Comparison is case sensitive ("stop" only)

Concept Explanation

Words are read one by one.
We print each word until we see "stop".
When "stop" appears, we stop reading and do not print it.

Step-by-Step Explanation

1.Start an infinite loop.

2.Read a word using input().

3.If the word is exactly "stop", break the loop.

4.Otherwise, print the word.

5.Continue until "stop" is found.

Concept Explanation

Words are read one by one.
We print each word until we see "stop".
When "stop" appears, we stop reading and do not print it.

Step-by-Step Explanation

1.Start an infinite loop.

2.Read a word using input().

3.If the word is exactly "stop", break the loop.

4.Otherwise, print the word.

5.Continue until "stop" is found.

Input / Output Format

Input Format
• Words are provided one by one (each word on a new line)

• Input ends when the word "stop" is read
Output Format
• Print each word on a new line

• Do not print "stop"
Constraints
• Each word length ≤ 10^5

• Comparison is case sensitive ("stop" only)

Examples

Input:
hello world python stop
Output:
hello world python

Example Solution (Public)

Python
import sysnw=sys.stdin.read().strip().split()nif not w: sys.exit(0)nout=[]nfor tok in w:n  if tok=='stop':n    breakn  out.append(tok)nsys.stdout.write(' '.join(out))

Official Solution Code

import sysnw=sys.stdin.read().strip().split()nif not w: sys.exit(0)nout=[]nfor tok in w:n  if tok=='stop':n    breakn  out.append(tok)nsys.stdout.write(' '.join(out))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.