{"id":5068,"date":"2021-11-27T00:01:11","date_gmt":"2021-11-27T08:01:11","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5068"},"modified":"2021-12-04T09:11:27","modified_gmt":"2021-12-04T17:11:27","slug":"the-string-span-functions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5068","title":{"rendered":"The String Span Functions"},"content":{"rendered":"<p>When I scour the C for functions to assist my code, if often overlook a few that seem dull or confusing. One of those I&#8217;ve always glossed over is the <em>strspn()<\/em> function, as well as its counterpart, <em>strcspn()<\/em>.<br \/>\n<!--more--><br \/>\nThese functions scan a string for the first occurrence of a given character &mdash; or for not of a given character.<\/p>\n<p>For example, if you want to discover where the first comma in a string is located, you use this statement:<\/p>\n<p><code>r = strcspn(\"Hello, world!\",\",\");<\/code><\/p>\n<p>The return value <code>r<\/code> contains 5, which is the offset (starting with zero) where the comma is found in the string (<em>char<\/em> array).<\/p>\n<p>Here are the two functions&#8217; prototypes:<\/p>\n<p><code>size_t strspn(const char *s, const char *charset);<br \/>\nsize_t strcspn(const char *s, const char *charset);<\/code><\/p>\n<p>The first argument, <code>s<\/code>, is the string to search. The second argument, <code>charset<\/code>, is a string containing search characters. The return value is the offset within <code>s<\/code> where one of the characters in <code>charset<\/code> is either not found for <em>strspn()<\/em> or not found for <em>strcspn()<\/em>.<\/p>\n<p>Both functions are prototyped in the <code>string.h<\/code> header file.<\/p>\n<p>The <em>strspn()<\/em> function keeps looking through the first string as long as characters in the second string are found. Once a character in the first string doesn&#8217;t exist in the second, its offset is returned. The following code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_11_27-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2021_11_27-Lesson-a.c<\/a><\/h3>\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 a[] = \"HELLO, WORLD!\";\r\n    char b[] = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\r\n    size_t r;\r\n\r\n    r = strspn(a,b);\r\n    printf(\"%zu = '%c'\\n\",r,a[r]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 10, the <em>strspn()<\/em> function scans string <code>a[]<\/code>. As long as it finds characters in string <code>b[]<\/code> (the uppercase alphabet), it&#8217;s happy. When a non-matching character is encountered, its offset (starting at zero) is returned in variable <code>r<\/code>. This value is output, along with the offending character:<\/p>\n<p><code>5 = ','<\/code><\/p>\n<p>The <em>strcspn()<\/em> function does the opposite of <em>strspn()<\/em>: It scans the first string until it finds a character available in the second:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_11_27-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2021_11_27-Lesson-b.c<\/a><\/h3>\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 a[] = \"Hello, world!\";\r\n    char b[] = \" ,!\";\r\n    size_t r;\r\n\r\n    r = strcspn(a,b);\r\n    printf(\"%zu = '%c'\\n\",r,a[r]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Above, string <code>b[]<\/code> contains the non-alphabetic characters from string <code>a[]<\/code>. The <em>strcspn() <\/em>function at Line 10 searchings string <code>a[]<\/code> for the first occurrence of any of these characters. Here&#8217;s the output:<\/p>\n<p><code>5 = ','<\/code><\/p>\n<p>The output is the same as the first example, though in this case the purpose of string <code>b[]<\/code> is changed: It contains characters that stop the scan. Array element <code>a[5]<\/code> is the comma, which stops the scan after the &#8220;o&#8221; in &#8220;Hello.&#8221;<\/p>\n<p>I can think of a few practical uses for both the <em>strspn()<\/em> and <em>strcspn()<\/em> function, though I&#8217;m more attracted to the <em>strcspn()<\/em> function. In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5072\">next week&#8217;s Lesson<\/a> I explore one way to use this function to plow through a string and pluck out words.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Standard library functions <em>strspn<\/em> and <em>strcspn<\/em> are oddballs that you may find useful. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5068\">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-5068","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\/5068","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=5068"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5068\/revisions"}],"predecessor-version":[{"id":5095,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5068\/revisions\/5095"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5068"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5068"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5068"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}