{"id":1769,"date":"2016-02-20T00:01:32","date_gmt":"2016-02-20T08:01:32","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1769"},"modified":"2016-02-13T09:32:13","modified_gmt":"2016-02-13T17:32:13","slug":"slicing-strings-with-strsep","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1769","title":{"rendered":"Slicing Strings with <em>strsep()<\/em>"},"content":{"rendered":"<p>The <em>strtok()<\/em> function is the traditional C library routine used to slice up a string. It has its limitations, however, so a more recent function was included in the library on some compilers to replace it: <em>strsep()<\/em>.<br \/>\n<!--more--><br \/>\nYour C compiler may not support <em>strsep()<\/em>. That&#8217;s fine; you can use <em>strtok()<\/em>, and you should if you want to write portable code. The <em>strsep()<\/em> function, however, scans for empty strings as well as multiple strings (an array of strings).<\/p>\n<p>The <em>strsep()<\/em> function is defined in the <code>string.h<\/code> header file. Here is the man page format:<\/p>\n<pre><code>char * strsep(char **stringp, const char *delim);<\/code><\/pre>\n<p>The first argument, <code>**stringp<\/code>, is one of those annoying pointer-pointer things; it&#8217;s the single string to search or an array of strings to search. The second argument, <code>*delim<\/code>, is a string of one or more separator characters, similar to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1758\">the <em>strtok()<\/em> function<\/a>.<\/p>\n<p>Like <em>strtok()<\/em>, the <em>strsep()<\/em> function returns a pointer to the first string yanked out of <code>**strngp<\/code>. Unlike <em>strtok()<\/em>, however, the function is called with the same arguments over and over until it returns a <code>NULL<\/code> pointer.<\/p>\n<p>Here is sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char *string,*found;\r\n\r\n    string = strdup(\"Hello there, peasants!\");\r\n    printf(\"Original string: '%s'\\n\",string);\r\n\r\n    while( (found = strsep(&string,\" \")) != NULL )\r\n        printf(\"%s\\n\",found);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 8 assigns text to pointer <code>string<\/code>. This is pretty much the safest way to assign the string, one that won&#8217;t generate any compiler warnings. Other methods might work, but also generate warnings or crash the code.<\/p>\n<p>Line 11 contains a <em>while<\/em> loop with the condition being the results of the <em>strsep()<\/em> function. As long as the function doesn&#8217;t return a <code>NULL<\/code>, the pointer <code>found<\/code> references the first chunk of text pulled out of <code>string<\/code>. The result it output at Line 12.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<pre><code>Original string: 'Hello there, peasants!'\r\nHello\r\nthere,\r\npeasants!<\/code><\/pre>\n<p>This function can be used as a solution for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1742\">this month&#8217;s Exercise<\/a>. In fact, it&#8217;s a tighter solution than using <em>strtok()<\/em> because the format for calling <em>strsep()<\/em> doesn&#8217;t change. So the entire thing can be put into a loop:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* parse words in the text *\/<\/span>\r\n    windex = 0;\r\n    while ( (*(words+windex) = strsep(&string,\" \")) != NULL)\r\n        windex++;<\/pre>\n<p>The <em>while<\/em> loop&#8217;s condition is a bit complex, but it&#8217;s effectively fetching each word in <code>string<\/code> and storing its location in the <code>words<\/code> array. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/02\/0220b.c\" rel=\"\">Click here<\/a> to view the entire <em>strsep()<\/em> solution to this month&#8217;s Exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>strsep()<\/em> function parses a string in a more thorough manner than <em>strtok()<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1769\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1769","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1769"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1769\/revisions"}],"predecessor-version":[{"id":1780,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1769\/revisions\/1780"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}