Strtok C

The C language of programming has a facility to split a string using the function strtok. This is done through a delimiter. It is an easy task that uses a pointer token. A string that is taken as input is divided into small strings called tokens. ‘S’ is the name that indicates that in “strtok”, str stands for the string, and “tok” stands for the token. So this function splits the string into tons of tokens. We have highlighted some examples in this tutorial that will help you out understand this concept.

Syntax

char * strtok(char str[], const char *delimiter);

Where char str[] is the string. And the second argument of this function is a delimiter. Const char *delims , through which splitting takes place.

Linux operating system provides the user facility to get help or search a guide for their consultation. This is a guide manual, a built-in feature of the Ubuntu terminal. Simply use:

$ Man strtok

An interface will be opened; this shows basic description, usage, and examples regarding the understanding of the user.

Example 1

Here comes the first example of the strtok concept. In this code, we use two libraries required for the execution of the source code. The string.h is the library that supports all the functions of string in the C language. Strtok converts the big string into small parts. That’s why we need a string as an input in our code so that the output should be small pieces of that string known as tokens of the string. The string contains hyphens between two words.

The other inputs we use are the tokens. They are pointers in actuality, as their function is to highlight the point to be separated. These tokens help in separating the parts of the string. In this token, creation strtok() is used. That takes the string as an input in the parameter and the hyphen to show the point of separation from the token.

Char* token = strtok (str, “-“);

After the declaration of the strtok() function. For displaying all the small pieces of string, we need to use a while loop. So that it outputs the result by applying the respective condition. The condition is to print the string pieces until the token contains no value.

If we wish to get the output in Ubuntu. It will be done through the terminal. Just open it and write a piece of command. This command first indicates the compilation of the code, so we need a compiler. That is GCC. This code saves the output through –o in the output file through the source file. After compilation, execute the file through a command:

$ GCC –o file8 file8.c

$ ./file8         

From the output, you can see that the input string is converted into a small number of strings. This is done through the token, which cuts the string where the hyphen sign is present. We can also utilize any other sign for identification.

Example 2

This example is the same as the previous one. We have used the same sign (-) in the input string and the spaces in this illustration. In strtok() we do not add the string as a delimiter; we only use the hyphen. In this C code, the delimiter is stored in a variable and then used in the code.

Tok = strtok (a,s);

Strtok () function will use the while loop always to print the results on the screen. Unless there is no word in the character array, the loop will execute.

Again use the same compilation and execution method. Then you will get the output shown below.

From the output, you can see that space is added at the start of the tokens. This is because we don’t mention space characters in the delimiter declaration except for the hyphen.

Example 3

This example has the involvement of CSV (comma-separated values). The input string is taken in a character array with a series of numbers in them with some parameters like DOB. The delimiter is defined as space. Where ever the space is present between the words, a comma will be added. And then, the words will be separated through commas in the output.

Tok = strtok (x ,s);

Whereas x is an array and s is the delimiter.

In this example, the functionality of the while loop is quite different. In previous ones, it only displays the token values splits from the string. But in this case, it not only shows the splits tokens plus adds the comma with them in a single line as the string was introduced in the input.

While( tok != 0)

{

Printf(%s ,” , tok);

Tok = strtok (0,s);

}

The output is displayed by compilation and execution of the code through the GCC compiler.

You can see that the string values that were having only space between them are now separated by commas.

Example 4

In this example, some other symbols are used instead of the hyphen as a delimiter in the code. The input string contains three symbols “, , : , ? “. These symbols are removed, and each token is created where these symbols are present, and these symbols are removed from the output.

P = strtok (string,”,: “);

Where p is the token pointer, a message is displayed that contains the input string. The while loop will display the results by removing the symbols. The token locates the delimiter, and through these delimiters, separate pieces are splits from the string.

While (p! = NULL)

{

Printf (%s\n” ,p);

P = strtok (NULL, “,:?”);

}

\n is to display the words/tokens in separate lines.

Now execute the code present in the file through compilation with the compiler.

$ GCC –o file8 file8.c

The output shows that the symbols are removed through strtok() in the code. And the output is free from the symbols.

Example 5

This is an example of removing the delimiters by replacing the new ones in the string. Two symbols that are “@, * are present in the string that are removed and are replaced by []. String and the token pointer are taken as input. In this code, there is another feature. As we have to add a symbol in the place of other symbols, so we need a counter to increment the character numbers and then add the symbol in that place. The token number is initiated as 0 at the start.

Char * token = strtok(src , delimeter);

Whereas src is the name of an array of the string. While loop will help in displaying the content of the strings.

While (token != Null)

{ printf (“d : [%s]\n” , ++toknum ,token);

}

In the body of the while statement. You can see that ‘[]’ are added to the token of a string as these brackets are present to each token, so here we need to add the counter to increment the value so that each token has the brackets.

Take the output by following the same method as described above.

From the output, you can see that the symbols present in the string are removed, and each of the tokens is displayed in a separate line with the brackets around them.

Conclusion

The strtok() splits the string into small tokens through specific search criteria named delimiter. The examples of this article are sufficient that leads to an excess in increasing your knowledge.



from Linux Hint https://ift.tt/2UGtGsy

Post a Comment

0 Comments