class Solution {
public:
int lengthOfLastWord(const char *s) {
int len = strlen(s);
if (!len) return 0;
int n=0, i = len1;
while (s[i] == ' ') i;
for (; i >= 0; i,n++)
if (s[i] == ' ')
return n;
return n;
}
};
My C++ Solution

std::string will provide you with .length() .size() in constant time (C++11) where as in this case the plain strlen(s) is O(n). Basically you scan the string twice in the above solution.
 First with strlen() from begin to end '0'
 Second from end to first space character.
IMHO, key to tackling this problem (with speed) is to scan the string only once.
 First with strlen() from begin to end '0'

@chammika Haha, true. But he chose C, so he gets a bare char pointer. That makes it harder to solve this problem in one forward scan.