[leetcode]Length of Last Word

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int N = 0;
        const char *p = s;
        while(*p){
            p++;
            N++;
        }
        
        int i = N-1;
        while(i >= 0 && s[i] == ' '){
            i--;
        }
        if(i == -1) return 0;
        int end = i;
        
        while(i >= 0 && s[i] != ' '){
            i--;
        }
        int start = i;
        
        return end-start;
        
    }
};


posted @ 2013-07-30 18:44  坚固66  阅读(136)  评论(0编辑  收藏  举报