Return the shortest word in a sentence. Words are separated by single spaces. When two or more words share the shortest length, return the first (leftmost) one. Return null when the input is null or blank.
Examples:
Input: sentence = 'the quick brown fox'
Output: 'the'
Explanation: the and fox both have length 3, the appears first
Input: sentence = 'a bb ccc'
Output: 'a'
Explanation: a is the shortest word
Input: sentence = 'hello'
Output: 'hello'
Explanation: A single word is the shortest by default
Input: sentence = 'cat dog'
Output: 'cat'
Explanation: Both have length 3, cat appears first
Input: sentence = null
Output: null
Explanation: Null input returns null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay November 2026 (Nov 1–15)Sponsored by FLXBL
Contest runs November 1 - 15. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.