Skip to content Skip to sidebar Skip to footer

How to Clear Strtok So You Can Use It Again

The C language of programming has a facility to split up a string using the office strtok. This is done through a delimiter. Information technology is an easy task that uses a pointer token. A cord that is taken every bit input is divided into modest 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 cord into tons of tokens. We take highlighted some examples in this tutorial that will help you out sympathise this concept.

Syntax

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

Where char str[] is the string. And the 2nd argument of this part is a delimiter. Const char *delims , through which splitting takes identify.

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 characteristic of the Ubuntu terminal. Simply use:

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

Example i

Hither comes the first example of the strtok concept. In this code, we apply ii libraries required for the execution of the source lawmaking. The string.h is the library that supports all the functions of string in the C linguistic communication. Strtok converts the large string into small parts. That's why nosotros need a string every bit an input in our code so that the output should be small-scale pieces of that string known as tokens of the string. The string contains hyphens between 2 words.

The other inputs we use are the tokens. They are pointers in actuality, equally their part is to highlight the point to be separated. These tokens help in separating the parts of the string. In this token, cosmos 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. Information technology will be done through the terminal. Just open it and write a piece of command. This command first indicates the compilation of the lawmaking, so we need a compiler. That is GCC. This code saves the output through –o in the output file through the source file. Subsequently compilation, execute the file through a command:

$ GCC –o file8 file8.c

$ ./file8

From the output, you can run across that the input string is converted into a modest number of strings. This is washed through the token, which cuts the cord where the hyphen sign is present. Nosotros can also utilize any other sign for identification.

Example 2

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

Strtok () role will employ the while loop always to print the results on the screen. Unless there is no discussion in the character assortment, the loop will execute.

Again use the aforementioned compilation and execution method. And so y'all will get the output shown below.

From the output, y'all can meet that infinite 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.

Case 3

This case has the involvement of CSV (comma-separated values). The input cord is taken in a graphic symbol assortment with a series of numbers in them with some parameters like DOB. The delimiter is divers as space. Where ever the space is present betwixt the words, a comma will be added. And so, the words will exist separated through commas in the output.

Whereas x is an array and south is the delimiter.

In this case, 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 example, it non merely shows the splits tokens plus adds the comma with them in a single line as the cord was introduced in the input.

While( tok != 0 )

{

Printf("%south ," , tok);

Tok = strtok ( 0,southward);

}

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

Y'all tin can see that the string values that were having only space between them are at present separated by commas.

Example 4

In this case, 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 nowadays, and these symbols are removed from the output.

P = strtok (string,",: ");

Where p is the token pointer, a bulletin 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! = Zilch)

{

Printf ("%s\n" ,p);

P = strtok (Naught, ",:?");

}

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

At present execute the code nowadays in the file through compilation with the compiler.

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

Instance 5

This is an example of removing the delimiters past 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 lawmaking, there is some other feature. Every bit we have to add a symbol in the place of other symbols, so we need a counter to increment the grapheme numbers and then add the symbol in that place. The token number is initiated every bit 0 at the outset.

Char * token = strtok(src , delimeter);

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

While (token != Nil)

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

}

In the body of the while statement. You lot can run across that '[]' are added to the token of a string as these brackets are nowadays to each token, and then hither we need to add the counter to increment the value and then 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 split up line with the brackets effectually them.

Conclusion

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

Virtually the author

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

dejongrettest.blogspot.com

Source: https://linuxhint.com/strtok-c/

Publicar un comentario for "How to Clear Strtok So You Can Use It Again"