zoukankan      html  css  js  c++  java
  • 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;
        }
    
  • 相关阅读:
    Title
    Title
    Title
    Title
    Python生成随机验证码
    Time模块和datetime模块
    For循环的实质
    Python函数
    集合set
    字符串内置方法的使用
  • 原文地址:https://www.cnblogs.com/bingo2-here/p/7942716.html
Copyright © 2011-2022 走看看