1

I need an efficient function that extracts first second and rest of the sentence into three variables.

6
  • 1
    Tis a duplicate - stackoverflow.com/questions/1483206/… - but not exact
    – ChrisF
    Oct 12, 2009 at 20:19
  • "on-disk" is one word or two words?
    – sambowry
    Oct 12, 2009 at 20:29
  • Do you mean that the algorithm would take "abcd" and split it into 'a', 'b', and "cd" ?
    – Alex Moore
    Oct 12, 2009 at 21:46
  • 1
    No. "cat eats many rats" should give "cat", "eats" and "many rats" Oct 12, 2009 at 22:14
  • Ok then, my solution should work. Give it a try.
    – Alex Moore
    Oct 12, 2009 at 22:16

3 Answers 3

12

Easy way: Use strtok() or strtok_r to get the first two tokens, which will remove them from the string, so the string itself will be your third token you were looking for.

Hard way: Parse it yourself :(

Strtok is in the C string library, and will mutate your original string so be careful, copy the string first if it needs to remain intact.

Possible Example:

//#include <string.h>

char input[] ="first second third forth";
char delimiter[] = " ";
char *firstWord, *secondWord, *remainder, *context;

int inputLength = strlen(input);
char *inputCopy = (char*) calloc(inputLength + 1, sizeof(char));
strncpy(inputCopy, input, inputLength);

firstWord = strtok_r (inputCopy, delimiter, &context);
secondWord = strtok_r (NULL, delimiter, &context);
remainder = context;

printf("%s\n", firstWord);
printf("%s\n", secondWord);
printf("%s\n", remainder);

getchar();
free(inputCopy);

This should work just fine and be threadsafe with the original string unmutated.

5
  • 1
    Another gotcha is that strtok() uses an internal static variable, so it is not thread safe. Use strtok_r() if that's an issue. Oct 12, 2009 at 20:33
  • Very true, changed example to use strtok_r
    – Alex Moore
    Oct 12, 2009 at 20:51
  • 2
    Its giving me first token in reminder and not the remaining sentence. Oct 12, 2009 at 22:20
  • remainder will hold "first" here.
    – AJ.
    Oct 13, 2009 at 9:27
  • Ahh, I played with it when I got home last night and fixed the example. Try it now, it should work.
    – Alex Moore
    Oct 13, 2009 at 14:33
2

You need to define the delimiters first. There are a few problems with strtok (it modifies its argument, for one, which may land you in trouble). I prefer to read in the string and run a custom parser which may range from sscanf to a full-blown parser. Please post some more detail.

1
  • +1 sscanf() would work well, since I expect "words" means "anything not whitespace," making the conversion specifier easy, and should neatly avoid all the problems with strtok() and friends.
    – Chris Lutz
    Oct 13, 2009 at 14:42
1

strtok()

2
  • 1
    @BartłomiejSemańczyk Actually, if you take out the link it still says strtok, so it's still an answer.
    – MicroVirus
    Nov 21, 2015 at 12:02
  • @BartłomiejSemańczyk I answered this in 09, link still works! Dec 3, 2015 at 3:50

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.