58. 最后一个单词的长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int lengthOfLastWord(char * s){
if(strlen(s) == 0)
return 0;
int x =strlen(s)-1;
while(s[x]==' '){
x-=1;
if(x<0)
return 0;
}
int j = 0;
while(s[x]!= ' '){
x-=1;
j+=1;
if(x<0)
return j;
}
return j;
}

回头学下KMP