```
class Solution {
public:
int lengthOfLastWord(const char *s) {
int n = strlen(s);
int res = 0, ret = 0;
for(int i = n-1; i >=0; i--)
{
if(s[i] == ' ' && ret == 0)
continue;
if(s[i] != ' ')
{
res++;
ret = 1;
}
if(s[i] == ' ' && ret == 1)
break;
}
return res;
}
};
```