Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
For example,
Given s = "Hello World"
,
return 5
.
1 int lengthOfLastWord(const char *s) { 2 // Note: The Solution object is instantiated only once and is reused by each test case. 3 int l = strlen(s); 4 if(l == 0) 5 return 0; 6 int result = 0; 7 while(s[l-1] == ' ' && l > 0) 8 l--; 9 while(s[l-1] != ' ' && l > 0){ 10 l--; 11 result++; 12 } 13 return result; 14 }