Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is the null character included in the return value of the function strtok?

Tags:

c

string

For the string returned by strtok, does it have a \0 at the end? Or does it not, like the strncpy function?

I would check by copying the return value of strtok to another string array with strcpy, but then I wouldn't know if the appended \0 was the effect of strtok or strcpy (since strcpy does add \0).

edit: sorry, I can't "copy the return value to of strtok to another string array with strcpy".

like image 446
user81055 Avatar asked Jul 05 '13 03:07

user81055


People also ask

Does strtok return NULL?

strtok() returns a NULL pointer. The token ends with the first character contained in the string pointed to by string2. If such a character is not found, the token ends at the terminating NULL character. Subsequent calls to strtok() will return the NULL pointer.

Why do you call strtok with NULL?

When there are no tokens left to retrieve, strtok returns NULL, meaning that the string has been fully tokenized.

What does strtok () do in C?

The C function strtok() is a string tokenization function that takes two arguments: an initial string to be parsed and a const -qualified character delimiter. It returns a pointer to the first character of a token or to a null pointer if there is no token.

Does strtok include the delimiter?

Each call to strtok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte.


1 Answers

From cplusplus.com

This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function.

like image 74
Ran Eldan Avatar answered Sep 28 '22 16:09

Ran Eldan