C ++
strtok
and strcmp
are functions that belong to the header <cstring>
and have been added to the C ++ library for compatibility with C. Its use is not recommended as there are better and safer alternatives belonging to the < a href="https://en.wikipedia.org/wiki/Standard_Template_Library"> stl .
std::strtok
Used for a process known as tokenization , which is the process to divide a string of text into words, phrases, symbols or another group of elements, each element is called token .
Receives a pointer to the first character of the string to tokenize and a pointer to the first character of the string to be used as the delimiter:
char* strtok( char* cadena_a_tokenizar, const char* delimitador );
The result is a pointer to the first character after the first delimiter found, or 0
( NULL
) if the delimiter is not found. So this code:
char cadena[100] = "patatas#fritas#con#ketchup#y#mayonesa";
char *token = std::strtok(input, "#");
while (token)
{
std::cout << token << ' ';
token = std::strtok(NULL, "#");
}
Sample:
patatas fritas con ketchup y mayonesa
Keep in mind that strtok
changes the cadena_a_tokenizar
writing 0
( NULL
) in the chain as it encounters tokens and that strtok
is not safe to use in concurrency .
std::strcmp
It is a character string comparison function, it receives a pointer to the first character to compare from the first string and a pointer to the first character to be compared from the second string. The result is an integer:
int strcmp( const char *cadena_izquierda, const char *cadena_derecha );
The result can get the values -1
, 0
or 1
depending on the result of the comparison: cadena_izquierda
less than cadena_derecha
, cadena_izquierda
and cadena_derecha
equal or cadena_izquierda
greater than cadena_derecha
respectively.
The way in which it is determined if a chain is smaller or bigger than another is using lexicographical order and the ASCII value is generally used for each of the characters for this purpose; that is to say: the algorithm is case-sensitive, so the string:
PATATAS FRITAS CON KETCHUP Y MAYONESA
It is less than the string:
patatas fritas con ketchup y mayonesa
Because the letters uppercase have an ASCII value lower than the lowercase :
It is required that both cadena_izquierda
and cadena_derecha
are strings of characters ending with a null character, otherwise the behavior of the function will be undefined.
C
strtok
and strcmp
are functions that belong to the header <string.h>
and function in the same way as the one already described in the C ++ section 1 ; there are secure versions of both functions to which the _s
suffix has been added or the number of characters to be treated is limited:
strtok_s
It is the secure version of strtok
that adds parameters that facilitate control and help detect or avoid errors:
char *strtok_s(char *restrict cadena_a_tokenizar, rsize_t *restrict maximo,
const char *restrict delimitador, char **restrict estado);
The parameters of cadena_a_tokenizar
and delimitador
work the same as the version of C ++ already described 1 .
The parameter maximo
must point to a variable that contains the length of cadena_a_tokenizar
, the function strtok_s
will modify that variable with the characters that remain to be examined.
The parameter estado
is a pointer to a character in which the function strtok_s
will keep its internal state.
These improvements make strtok_s
safer and makes it possible to use it in concurrency contexts.
strncmp
It's the secure version of strcmp
that adds a parameter with the maximum number of characters to compare:
int strncmp( const char *cadena_izquierda, const char *cadena_derecha, size_t maximo );
This makes the function safer to use, otherwise it behaves in the same way as the C ++ version already described 1 .
Annex: the qualifier restrict
.
The qualifier restrict
seen in the function strtok_s
is an exclusive qualifier of C that is used over pointers. Indicates that for the pointer qualified as restrict
(or any address obtained through arithmetic of pointers through it), throughout its life cycle will be used to access the object it points to; this can help the compiler perform optimizations.
1 It's actually the other way around, the C ++ version works the same as C's, since these functions belong to C.