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.

 1 public class Solution {
 2     public int lengthOfLastWord(String s) {
 3         int len = s.length();
 4         int count=0;
 5         for(int i=len-1;i>=0;i--){
 6             if(s.charAt(i)==' '&&count==0)continue;
 7             else if(s.charAt(i)==' '){
 8                 break;
 9             }
10             else{
11                 count++;
12             }
13         }
14         return count;
15     }
16 }
View Code

 

 

posted @ 2014-02-06 14:24  krunning  阅读(74)  评论(0编辑  收藏  举报