Skip to content

Commit

Permalink
最后一个单词长度
Browse files Browse the repository at this point in the history
  • Loading branch information
money89757 committed Jun 6, 2018
1 parent a1815d2 commit 87ecb94
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 算法/LeetCode/lengthOfLastWord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>

int lengthOfLastWord(char *s)
{
int len = 0;
while(*s != '\0')
{
if(s[-1] == ' ' && s[0] != ' ')
{
len = 1;
}

else if(*s != ' ')
{
len++;
}

s++;
}

return len;
}

int main(void)
{
char str[] = "Hello world";
int len = lengthOfLastWord(str);
printf("%d\n",len);

}

0 comments on commit 87ecb94

Please sign in to comment.