Practice Program in java Day-10

Today I learned the new topic of String:
find the length of last word in a string;

program :
class Solution {
public int lengthOfLastWord(String s) {
int last = s.length() – 1;

    while (last >= 0 && s.charAt(last) == ' ') {//to remove he spaces
        last--;
    }

    int start = last;
    while (start >= 0 && s.charAt(start) != ' ') {
        start--;
    }

    return last - start;        
}
public static void main(String... args){
    String a="fly of moon ";
    int S=lengthOfLastWlrd(a);
    System.out.println(S);}

Enter fullscreen mode Exit fullscreen mode

}
}

原文链接:Practice Program in java Day-10

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容