Back to DSA sheet
Length of Last Word
EasyArraysGiven a string of words separated by spaces, return the length of the last word. There may be extra spaces, including trailing ones.
Input format (stdin): a single line of text. Output the length of the last word.
Examples
Input: Hello World
Output: 5
The last word is 'World'.
Input: fly me to the moon
Output: 4
The last word is 'moon'.
Constraints
- 1 <= length <= 10^4
- Letters and spaces only, at least one word
Sheets
NeetCode 250
length-of-last-word.cpp3 sample tests
Loading editor
Test results
Run the sample tests to check your solution against expected output.
Custom input (stdin)
Output
Run your code to see its output.