diff options
Diffstat (limited to 'Computer_Science/leetcode/58-length_of_last_word.c')
| -rw-r--r-- | Computer_Science/leetcode/58-length_of_last_word.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Computer_Science/leetcode/58-length_of_last_word.c b/Computer_Science/leetcode/58-length_of_last_word.c new file mode 100644 index 0000000..e1a9b97 --- /dev/null +++ b/Computer_Science/leetcode/58-length_of_last_word.c @@ -0,0 +1,17 @@ +int lengthOfLastWord(char* s) { + char* space; + while(*s == ' ') + s++; + + if(*s == '\0') return 0; + space = s; + while(*s != '\0') { + if(*s == ' ' && *(s + 1) != ' ' && *(s + 1) != '\0') + space = s; + s++; + } + + while(*(--s) == ' '); + + return *space == ' ' ? s - space : s - space + 1; +} |
