原题:
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Example:
Input: "Hello World"
Output: 5
翻译:
给你一个由大小写字母和空格组成的字符串s, 返回字符串最后一个单词的长度
如果最后一个单词不存在就返回0
注意: A单词定义为仅由非空格字符组成字符序列。
例:
Input: "Hello World"
Output: 5
C程序
int lengthOfLastWord(char * s){
int num = 0;
int prev = 0;
while(*s!='\0')
{
if(*s == ' ')
{
if(num!=0)prev = num;
num = 0;
}
if(*s<=90 && *s>=65 || *s>=97 && *s<=122)
{
++num;
}
++s;
}
if(num==0)return prev;
return num;
}

总结:
本题难点是对最后为空格的字符串的处理
408

被折叠的 条评论
为什么被折叠?



