Forums
New posts
Search forums
News
Security News
Technology News
Giveaways
Giveaways, Promotions and Contests
Discounts & Deals
Reviews
Users Reviews
Video Reviews
Support
Windows Malware Removal Help & Support
Inactive Support Threads
Mac Malware Removal Help & Support
Mobile Malware Removal Help & Support
Blog
Log in
Register
What's new
Search
Search titles only
By:
Search titles only
By:
Reply to thread
Menu
Install the app
Install
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Guides
Programming Guides & Questions
In C, how does strtok() break a string into tokens?
Message
<blockquote data-quote="Bot" data-source="post: 1028268" data-attributes="member: 52014"><p>`strtok()` is a function from the C standard library that is used to break a string into a series of substring or tokens on the basis of a specified delimiter. Here is how it works:</p><p></p><p>In the example code you provided, the string `" - This, a sample string."` is passed to strtok() along with the specified delimiter `" ,.-"`. The delimiter is a string that contains all characters that can be used as delimiters. These characters will be used by strtok() to split the input string into a series of tokens.</p><p></p><p>Strtok() returns the first token in the input string, which in this case is "-". The first parameter is the string to be tokenized, and the second parameter is the set of delimiters that defines where tokens will be separated.</p><p></p><p>On a subsequent call to strtok() with a NULL string pointer as its first argument, the function resumes scanning the same string, and starts searching for the next token following the termination point of the previous one.</p><p></p><p>In the while loop, the function continues to call strtok() using a NULL pointer as the first argument until there are no more tokens left in the string which is indicated by a NULL return value.</p><p></p><p>So, the output you see on the screen is the individual tokens or substrings separated by the specified delimiter.</p><p></p><p>In the example code you provided, "Splitting string "- This, a sample string." into tokens:" is displayed as the first line of output. Then on the next line "- This" is printed, followed by "a", "sample" and "string". These are individual substrings or tokens defined by the delimiters specified.</p></blockquote><p></p>
[QUOTE="Bot, post: 1028268, member: 52014"] `strtok()` is a function from the C standard library that is used to break a string into a series of substring or tokens on the basis of a specified delimiter. Here is how it works: In the example code you provided, the string `" - This, a sample string."` is passed to strtok() along with the specified delimiter `" ,.-"`. The delimiter is a string that contains all characters that can be used as delimiters. These characters will be used by strtok() to split the input string into a series of tokens. Strtok() returns the first token in the input string, which in this case is "-". The first parameter is the string to be tokenized, and the second parameter is the set of delimiters that defines where tokens will be separated. On a subsequent call to strtok() with a NULL string pointer as its first argument, the function resumes scanning the same string, and starts searching for the next token following the termination point of the previous one. In the while loop, the function continues to call strtok() using a NULL pointer as the first argument until there are no more tokens left in the string which is indicated by a NULL return value. So, the output you see on the screen is the individual tokens or substrings separated by the specified delimiter. In the example code you provided, "Splitting string "- This, a sample string." into tokens:" is displayed as the first line of output. Then on the next line "- This" is printed, followed by "a", "sample" and "string". These are individual substrings or tokens defined by the delimiters specified. [/QUOTE]
Insert quotes…
Verification
Post reply
Top