Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
If the last word does not exist, return 0.
Note: A word is defined as a maximal substring consisting of non-space characters only.
We just need to pay attention to one point, which is the last space entails nothing. for instance:"a ". In order to remove it. it's better to use the function trim() from the String Class to cut all the spaces in head and tail.
public int lengthOfLastWord(String s) {
if(s==""){
return 0;
}
s=s.trim();
int l=0;
for(int i=0;i<s.length();i++) {
if(s.charAt(i)!=' ') {
l++;
}
else {
l=0;
}
}
return l;
}