diff --git a/c/58-Length-Of-The-Last-Word.c b/c/58-Length-Of-The-Last-Word.c new file mode 100644 index 000000000..769667fb8 --- /dev/null +++ b/c/58-Length-Of-The-Last-Word.c @@ -0,0 +1,25 @@ +/* +Given a string s consisting of words and spaces, return the length of the last word in the string. + +Space: O(1) +Time: O(n) +*/ + +int lengthOfLastWord(char * s){ + int last_space=-1; // Index of the last ' ' + int last_word=0; // Length of the last word + int i; + for (i=0; s[i]!='\0'; i++) { + if (s[i]==' ') { + if (last_space != (i-1)) { + last_word = i-last_space -1; + } + last_space = i; + } + } + if (last_space == (i-1)) { // if the length wanted is already in last_word + return last_word; + } else { + return i-last_space-1; + } +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: