```
class Solution {
public:
int lengthOfLastWord(string s) {
int n=s.size();
int count =0;
for(int i=0;i<n;){
while(i<n&&s[i]!=' '){count++;i++;}
while(i<n&&s[i]==' ')i++;
if(i<n)count=0;
}
return count;
}
};
```