4
\$\begingroup\$

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;
}
\$\endgroup\$
3
  • \$\begingroup\$ Not sure how this works: "b a " 2 \$\endgroup\$ Commented Mar 9, 2013 at 17:44
  • \$\begingroup\$ @LokiAstari, it should be "ba " 2 \$\endgroup\$
    – Fihop
    Commented Mar 9, 2013 at 17:59
  • \$\begingroup\$ For an interview question, surely an important part is explaining how it works, so how about some comments? \$\endgroup\$ Commented Mar 10, 2013 at 22:48

3 Answers 3

4
\$\begingroup\$

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;
}
\$\endgroup\$
3
\$\begingroup\$

(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).

\$\endgroup\$
5
  • \$\begingroup\$ sorry for confusion. I've edited the question. It should return the length of last non-empty word. \$\endgroup\$
    – Fihop
    Commented Mar 9, 2013 at 16:27
  • \$\begingroup\$ I added and answer that extends your solution to ignore trailing spaces. Hope you don't mind... \$\endgroup\$ Commented Mar 9, 2013 at 18:31
  • \$\begingroup\$ @WilliamMorris: Of course not, +1, thanks! On the other hand, content on Stack Exchange is under Creative Commons (AFAIK), so it's legal too :) \$\endgroup\$
    – palacsint
    Commented Mar 9, 2013 at 19:00
  • \$\begingroup\$ BTW, I think I have earned more points from this cut-and-paste of your work than for any of my reviews :-) \$\endgroup\$ Commented Mar 12, 2013 at 16:36
  • \$\begingroup\$ @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/… \$\endgroup\$
    – palacsint
    Commented Mar 12, 2013 at 20:55
3
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.