Length of Last Word
int lengthOfLastWord(const char *s) {
// Note: The Solution object is instantiated only once and is reused by each test case.
const char* cur = s, *pStart;
while(*cur==' ') //skip white space
cur++;
if(*cur=='\0')
return 0;
pStart = cur;
int len = 0;
while(*cur!='\0')
{
if(*cur==' ')
{
if(*(cur-1)!=' ')
{
len = cur - pStart;
pStart = cur+1;
}else
pStart++;
}
cur++;
}
if(*(cur-1)!=' ') //don't forget
len = cur-pStart;
return len;
}
浙公网安备 33010602011771号