5

how would you get the last word of a string, starting from the '\0' newline character to the rightmost space? For example, I could have something like this where str could be assigned a string:

char str[80];
str = "my cat is yellow";

How would I get yellow?

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
user1000219
  • 115
  • 1
  • 2
  • 6

5 Answers5

11

Something like this:

char *p = strrchr(str, ' ');
if (p && *(p + 1))
    printf("%s\n", p + 1);
cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

In case you don't want to use 'strrchr' function, Here is the solution.

i = 0;
char *last_word;

while (str[i] != '\0')
{
    if (str[i] <= 32 && str[i + 1] > 32)
        last_word = &str[i + 1];
    i++;
}
i = 0;
while (last_word && last_word[i] > 32)
{
    write(1, &last_word[i], 1);
    i++;
}
Sbk3824
  • 1,229
  • 1
  • 14
  • 24
0

I would use function strrchr()

il_guru
  • 8,383
  • 2
  • 42
  • 51
mikithskegg
  • 806
  • 6
  • 10
-2

The best way to do this is to take advantage of existing solutions. One such solution (to a much more general problem) is Perl Compatible Regular Expressions, an open-source regular expression library for C. So, you can match the string "my cat is yellow" with the regular expression \b(\w+)$ (expressed in C as "\b(\w+)$") and keep the first captured group, which is "yellow."

Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
-2

(heavy sigh) The original code is WRONG in standard / K&R / ANSI C! It does NOT initialize the string (the character array named str)! I'd be surprised if the example compiled. What your program segment really needs is

if strcpy(str, "my cat is yellow")
    {
    /* everything went well, or at least one or more characters were copied. */
    }

or, if you promised not to try to manipulate the string, you could use a char pointer to the hard-coded "my cat is yellow" string in your source code.

If, as stated, a "word" is bounded by a space character or a NULL character, then it would be faster to declare a character pointer and walk backwards from the character just before the NULL. Obviously, you'd first have to be sure that there was a non-empty string....

#define NO_SPACE 20
#define ZERO_LENGTH -1

int iLen;
char *cPtr;

if (iLen=strlen(str) ) /* get the number of characters in the sting */
    { /* there is at least one character in the string */
    cPtr = (char *)(str + iLen); /* point to the NULL ending the string */
    cPtr--; /* back up one character */
    while (cPtr != str)
        { /* make sure there IS a space in the string
             and that we don't walk too far back! */
        if (' ' == *cPtr)
            { /* found a space */
/* Notice that we put the constant on the left?
   That's insurance; the compiler would complain if we'd typed = instead of == 
 */
            break;
            }
        cPtr--; /* walk back toward the beginning of the string */
        }
    if (cPtr != str)
        { /* found a space */
        /* display the word and exit with the success code */
        printf("The word is '%s'.\n", cPtr + 1);
        exit (0);
        }
    else
        { /* oops.  no space found in the string */
        /* complain and exit with an error code */
        fprintf(STDERR, "No space found.\n");
        exit (NO_SPACE);
        }
    }
else
    { /* zero-length string.  complain and exit with an error code. */
    fprintf(STDERR, "Empty string.\n");
    exit (ZERO_LENGTH);
    }

Now you could argue that any non-alphabetic character should mark a word boundary, such as "Dogs-chase-cats" or "my cat:yellow". In that case, it'd be easy to say

if (!isalpha(*cPtr) )

in the loop instead of looking for just a space....

Ernest_CT
  • 62
  • 4
  • uncompilable code as is; terribly convoluted and way way over commented. – Ahmed Masud Feb 10 '12 at 06:19
  • Comments are included for the benefit of both people new to C and (in a production environment) to the programmers who must maintain the code. Remember, the size of the comments has zero to do with the size of the executable. – Ernest_CT Feb 10 '12 at 06:33
  • If you believe the code to be uncompilable, you should feel free to explain what is wrong. Sniping does no good. – Ernest_CT Feb 10 '12 at 06:34
  • for anyone who is learning to program your snippet teaches them bad habits. (for example printing errors out on stdout using printf); using arbitrary exit code; poor choice of variable names; horrible use of the while loop; incorrect placement of decrement (actual semantic mistake in logic). then it is not actually bundled in a function of any sort so code is not compilable. For prod: C programmers understand C; explaining code line by line is a waste; a brief desc. of function will do; if there is some black art stuff, most QA would reject that unless absolutely necessary. – Ahmed Masud Feb 10 '12 at 06:41
  • i am not sniping at you, you are trying to help and that is very much appreciated. I am simply pointing out your errors so that you can correct them too. – Ahmed Masud Feb 10 '12 at 06:42
  • When people believe that they have a better way of completing some task, they should provide examples. For example, – Ernest_CT Feb 10 '12 at 06:53
  • For example, if I wanted to be perfectly correct, I would #define specific error codes and fprintf to STDERR. Do you need examples? – Ernest_CT Feb 10 '12 at 07:15
  • The first decrement of the variable cPtr is correct. (What IS missing in the loop is ANOTHER decrement. That's where the logic error lies.) cPtr is first assigned the address of the '\0' in the string, then decremented on the following line of code, so that it points at the presumed last character in the string. Please remember that a pre-increment, post-increment, pre-decrement, or post-decrement can appear virtually anywhere. The programming convention is to use a post-decrement (or post-increment) when no other work is being done with the variable (e.g., no comparison). – Ernest_CT Feb 10 '12 at 07:16
  • There ave various standard conventions for naming variables; one is to use a lowercase letter (indicating variable type) as the first character of the variable name, followed by an uppercase letter to begin the specific name. For example, iLen should be a hint that the variable contains an integer value and is apt to be some kind of length. Likewise, cPtr probably is a pointer to a character (whether variable or constant!). (There is a name for this kind of notation: Hungarian Notation. An Internet search will give you some examples.) – Ernest_CT Feb 10 '12 at 07:17