Leetcode 58 Length of Last Word 难度:0

https://leetcode.com/problems/length-of-last-word/

int lengthOfLastWord(char* s) {
    int ans = 0;
    int fans = 0;
    for(int i = 0;s[i];i++){
        if (s[i] ==' '){fans = ans;ans = 0;while(s[i + 1] == ' '){i++;}}
        else ans++;
    }
    return ans?ans:fans;
}

  

 

posted @ 2015-08-25 16:48  雪溯  阅读(97)  评论(0编辑  收藏  举报