160. Find the Length of Last Word

EasyStringString

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

Trailing spaces should be ignored.

Input: A single string s.

Output: An integer — the length of the last word.

Examples

Example 1
Input: Hello World
Output: 5
Explanation: The last word is 'World' which has length 5.
Example 2
Input:    fly me   to   the moon  
Output: 4
Explanation: Ignoring trailing spaces, the last word is 'moon' with length 4.
Example 3
Input: luffy is still joyboy
Output: 6
Explanation: The last word is 'joyboy' with length 6.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →