Length of Last Word

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.

Example:

Input: "Hello World"
Output: 5

public int lengthOfLastWord(String s) {
        if (s==null||s.length()==0){
            return 0;
        }
        s=s.trim();
        int tmp=0;
        char[] chars = s.toCharArray();
        for (int i=0;i<chars.length;i++){
            if (chars[i]!=' '){
                tmp++;
            }
            else{
                tmp = 0;
            }
        }
        return tmp;
    }
原文地址:https://www.cnblogs.com/bingo2-here/p/7942716.html