Length of Last Word

    int lengthOfLastWord(const char *s) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        const char* cur = s, *pStart;
        while(*cur==' ')  //skip white space
            cur++;
        if(*cur=='\0')
            return 0;
        pStart = cur;
        int len = 0;
        while(*cur!='\0')
        {
            if(*cur==' ')
            {
                if(*(cur-1)!=' ')
                {
                    len = cur - pStart;
                    pStart = cur+1;
                }else
                    pStart++;
            }
            
            cur++;
        }
        
        if(*(cur-1)!=' ')  //don't forget
            len = cur-pStart;
        
        return len;
    }

  

posted @ 2013-10-21 22:07  summer_zhou  阅读(101)  评论(0编辑  收藏  举报