From 79a9c52fa923fc78074d88463449a8b7f95ca3ef Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 26 Dec 2017 01:33:40 +0800 Subject: update leetcode solution --- Computer_Science/leetcode/58-length_of_last_word.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Computer_Science/leetcode/58-length_of_last_word.c (limited to 'Computer_Science/leetcode/58-length_of_last_word.c') 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; +} -- cgit v1.2.3