# Length of last word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example:

s               result

"Hello World"    5
"a "             1
" a "            1
"  ba "          2
"ba    "         2


The following is my code:

int lengthOfLastWord(const char* s)
{
const char* end=s;
while (*end != '\0')
{
++end;
}
--end;

while ((end >= s) && (*end == ' '))
{
--end;
}

const char* start = end;
while ((start >= s) && (*start != ' '))
{
--start;
}

return end-start;
}

• Not sure how this works: "b a " 2 – Martin York Mar 9 '13 at 17:44
• @LokiAstari, it should be "ba " 2 – Fihop Mar 9 '13 at 17:59
• For an interview question, surely an important part is explaining how it works, so how about some comments? – Peter Taylor Mar 10 '13 at 22:48

This is just the @palacsint solution extended to ignore trailing spaces.

int lengthOfLastWord2(const char* input)
{
int result = 0;
int last_result = 0;

while (*input != '\0') {
if (*input != ' ') {
result++;
} else if (result) {
last_result = result;
result = 0;
}
input++;
}
return result ? result : last_result;
}


(Just a quick note, I don't have too much time now.) Here is another approach:

int lengthOfLastWord2(const char* input)
{
int result = 0;
while (*input != '\0') {
if (*input != ' ') {
result++;
} else {
result = 0;
}
input++;
}

return result;
}


Please note that it returns zero when the last character is a space (it was not specified in the question).

• sorry for confusion. I've edited the question. It should return the length of last non-empty word. – Fihop Mar 9 '13 at 16:27
• I added and answer that extends your solution to ignore trailing spaces. Hope you don't mind... – William Morris Mar 9 '13 at 18:31
• @WilliamMorris: Of course not, +1, thanks! On the other hand, content on Stack Exchange is under Creative Commons (AFAIK), so it's legal too :) – palacsint Mar 9 '13 at 19:00
• BTW, I think I have earned more points from this cut-and-paste of your work than for any of my reviews :-) – William Morris Mar 12 '13 at 16:36
• @WilliamMorris: I guess it's easier to read/understand then upvote shorter answers than the deeper and longer ones... There are only 19 Civic Duty and 5 Electorate badges awarded on the site, so I see a lot of space for improvement :) Related: meta.codereview.stackexchange.com/questions/612/… – palacsint Mar 12 '13 at 20:55

All of the other answers assume "space" is only ' ', when in fact it can be \f, \n, \t \r or \v (see man isspace). The original question states "empty space characters" which implies there is more than one meaning (to me) that it is including all space characters. Some of the examples previous included would fail

"joseph\n"
"joseph\v"


counting this as 7 characters instead of 6.

So I thought I would rewrite this using the isspace standard library function, and test it against these other cases as well.

#include <stdio.h>
#include <string.h>

int lengthOfLastWord3(const char *input )
{
const char *end = input;
const char *last_word_start = NULL;
const char *last_word_end = NULL;
char prev_char = '\0';
int word_length = 0;

while ( *end != '\0')
{
if ( !isspace( *end ) &&
( isspace( prev_char ) || ( prev_char == '\0' )))
{
last_word_start = end;
last_word_end = end+1;
}
else if ( !isspace( prev_char ) && ( isspace( *end ) ) )
{
last_word_end = end;
}
else if ( !isspace( prev_char ) && ( !isspace( *end ) ) )
{
last_word_end = end+1;
}

prev_char = *end;

end++;
}

if ( last_word_start )
{
word_length = last_word_end - last_word_start;
}

return( word_length );
}


This works for a bunch of additional cases, including entries where \n \t \v \f are used.

New contributor
Keitai Otaku is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.