#C12917. Finding the Longest Word in a String
Finding the Longest Word in a String
Finding the Longest Word in a String
You are given one or more lines of input. Each line contains a list of words separated by spaces. Your task is to find and output the longest word from the first valid line of input. A valid line is one where every word consists solely of alphabetic characters (a–z and A–Z). If any word contains non-alphabet characters, you must print the error message Invalid input. Please enter words containing letters only.
and then read the next line. In the event of multiple words having the same maximum length, output the first one encountered.
Note: Input is read from standard input (stdin) and output should be written to standard output (stdout). Your solution should continue reading lines until a valid input is provided.
The formula for checking a valid word is given by: \( word \in [a-zA-Z]+ \).
inputFormat
The input consists of one or more lines. Each line contains words separated by spaces. You must process these lines until you encounter a valid line where every word is comprised only of alphabet letters.
outputFormat
If an invalid line is encountered, print Invalid input. Please enter words containing letters only.
(followed by a newline) and then process the next line. Otherwise, for the first valid line, print the longest word. In case of multiple words with the maximum length, print the first one.
apple
apple