Easy 0 solved 0% acceptance

Longest Word

Find the longest word in a sentence.

Problem

Return the longest word in text (words are separated by spaces). If several words share the longest length, return the first one.

Implement longest_word(text).

Input

text: The sentence.

Output

Return the longest word, or an empty string if there are no words.

Constraints

0 ≤ length ≤ 10,000

Examples

Example 1
Input:
text = "I love programming"

Expected output:
expected = "programming"
Input:
std::string text = "I love programming";

Expected output:
std::string expected = "programming";
Input:
string text = "I love programming";

Expected output:
string expected = "programming";

Explanation

"programming" has 11 letters, more than the others.

Example 2
Input:
text = "cat dog cow"

Expected output:
expected = "cat"
Input:
std::string text = "cat dog cow";

Expected output:
std::string expected = "cat";
Input:
string text = "cat dog cow";

Expected output:
string expected = "cat";

Explanation

All are 3 letters; the first, "cat", wins.

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.