首页 > 代码库 > LeetCode Length of Last Word

LeetCode Length of Last Word

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        if (s == NULL) return 0;
        int mlen = 0, len = 0;
        bool inword = false;
        int pos = 0;
        char ch = 0;
        while ((ch = s[pos]) != \0) {
            if (ch !=  ) {
                if (!inword) inword = true;
                len++;
            } else if (inword) {
                if (len > 0) mlen = len;
                len = 0;
                inword = false;
            }
                pos++;
        }
        if (len > 0) mlen = len;
        return mlen;
    }   
};

水一发