int lengthOfLastWord(char* s)
{
int count = 0;
int i = strlen(s) - 1;
while (s[i] == ' ')
{
i--;
}//防止出现最后是空格情况
for (; s[i] != ' '; i--)
{
if (i == 0)//遍历到头就返回计数器,返回count+1为了防止数组越界
{
return count + 1;
}
count++;
}
return count;
}
int main()
{
char s[] = "dsa ";
printf("%d",lengthOfLastWord(s));
return 0;
}