Skip to content

Instantly share code, notes, and snippets.

@201power
Created April 15, 2014 06:54
Show Gist options
  • Save 201power/10708564 to your computer and use it in GitHub Desktop.
Save 201power/10708564 to your computer and use it in GitHub Desktop.
class Solution {
public:
int lengthOfLastWord(const char *s) {
int n=strlen(s);
int i=n-1,len=0;
if (n==0)
return 0;
while (i>=0) {
if ((s[i]>='A' && s[i]<='Z') || (s[i]>='a' && s[i]<='z')){
len++;
i--;
}
else if (len==0)
i--;
else if (s[i]!=' ')
return 0;
else
break;
}
if (len>0)
return len;
else
return 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment