Length of Last Word
Practice this coding question on Coding Gurukul.
Problem statement
A parcel printer receives a short address caption with extra spaces around or between its words. Its layout engine needs the length of the final word before it reserves a small field on the label. Your task Find the number of letters in the last word of the supplied line. A word is a maximal consecutive run of non-space letters. Important details Ignore trailing spaces when finding the last word. Multiple spaces between words do not create empty words. Count letters in the final word only, not its offset or the length of the trimmed line. Worked example For the line " river station ", the last word is station and contains seven letters. Leading and trailing spaces do not change the answer 7.
Input format
One full line containing English letters and ordinary spaces.
Output format
Print the length of the final word.
Constraints
1 <= line length <= 10000; the line contains at least one word.
Sample input
river station
Sample output
7
Sign in to run or submit a solution in C++, Java or Python.