Last active
October 2, 2024 21:18
-
-
Save efeciftci/9120921 to your computer and use it in GitHub Desktop.
An example that shows usage of strtok function in C programming language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The following description is from Linux Programmer's Manual (strtok(3)): | |
* | |
* #include <string.h> | |
* char *strtok(char *str, const char *delim); | |
* | |
* The strtok() function breaks a string into a sequence of zero or more | |
* nonempty tokens. On the first call to strtok() the string to be parsed | |
* should be specified in str. In each subsequent call that should parse | |
* the same string, str must be NULL. | |
* | |
* The delim argument specifies a set of bytes that delimit the tokens in | |
* the parsed string. The caller may specify different strings in delim | |
* in successive calls that parse the same string. | |
* | |
* Each call to strtok() returns a pointer to a null-terminated string | |
* containing the next token. This string does not include the delimiting | |
* byte. If no more tokens are found, strtok() returns NULL. | |
*/ | |
#include <stdio.h> | |
#include <string.h> | |
int main() | |
{ | |
char message[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; | |
char* word; | |
/* get the first word from the message, seperated by | |
* space character */ | |
word = strtok(message, " "); | |
printf("1st word: %s\n", word); | |
/* get the second word from the message, NULL must be | |
* used to get tokens from the previous string now */ | |
word = strtok(NULL, " "); | |
printf("2nd word: %s\n", word); | |
/* the following loop gets the rest of the words until the | |
* end of the message */ | |
while ((word = strtok(NULL, " ")) != NULL) | |
printf("Next: %s\n", word); | |
return 0; | |
} |
noice
thanks
Illustrative. Thanks!
why do we need to extract the second word seperately and what if the string contains only one word??
TYVM!
Thanks for this, really useful.
Thank you so much
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful, thanks