{"id":900,"date":"2014-08-23T00:01:06","date_gmt":"2014-08-23T07:01:06","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=900"},"modified":"2014-08-16T08:06:35","modified_gmt":"2014-08-16T15:06:35","slug":"search-for-that-string-again","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=900","title":{"rendered":"Search for That String Again"},"content":{"rendered":"<p>It&#8217;s straightforward to use the <em>strstr()<\/em> and related C library searching functions to locate a string in a pile of text. To locate the next occurrence of that string takes a bit more work.<br \/>\n<!--more--><br \/>\nAnd the good news is that it&#8217;s not a lot more work, just some resetting of pointers.<\/p>\n<p>Yes, pointers. Here is yet another situation where you must use pointers and not array notation to best accomplish a task in the C language.<\/p>\n<p>The <em>strstr()<\/em> function returns a pointer referencing the location of one string found within another string. To continue searching, you need to reset the starting pointer to a position immediately after the location where the text was found. Figure 1 attempts to illustrate this concept.<\/p>\n<div id=\"attachment_934\" style=\"width: 480px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-934\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/08\/0823-figure1.png\" alt=\"Figure 1. How strstr() can be used to continue searching through a string.\" width=\"470\" height=\"233\" class=\"size-full wp-image-934\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/08\/0823-figure1.png 470w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/08\/0823-figure1-300x148.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><p id=\"caption-attachment-934\" class=\"wp-caption-text\">Figure 1. How <em>strstr()<\/em> can be used to continue searching through a string.<\/p><\/div>\n<p>Here&#8217;s some sample code for re-searching a string. It&#8217;s based on the original <em>strstr()<\/em> example from an earlier <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=889\">Lesson<\/a>.<\/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 *haystack = \"Was this the face that launch'd a thousand ships, And burnt the topless towers of Ilium.\";\r\n    char *needle = \"the\";\r\n    char *location;\r\n\r\n    printf(\"Searching:\\n%s\\n\",haystack);\r\n    printf(\"Looking for: '%s'\\n\",needle);\r\n\r\n    location = strstr(haystack,needle);\r\n    if( location == NULL)\r\n    {\r\n        perror(\"Text not found\");\r\n        return(1);\r\n    }\r\n    else\r\n    {\r\n        printf(\"String was found at position %d.\\n\",\r\n            (int)location-(int)haystack+1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* reset the string starting pointer *\/<\/span>\r\n    haystack = location+1;\r\n    <span class=\"comments\">\/* search again *\/<\/span>\r\n    location = strstr(haystack,needle);\r\n    if( location == NULL)\r\n    {\r\n        perror(\"String not found a second time\");\r\n        return(1);\r\n    }\r\n    else\r\n    {\r\n        printf(\"String was found at again position %d.\\n\",\r\n            (int)location-(int)haystack+1);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The key is found at Line 26, where the <code>haystack<\/code> pointer is reset to the <code>location<\/code> pointer, the address where the string was found. But, the <code>haystack<\/code> pointer needs to be one position farther in the string. At Line 26 that was accomplished by adding one to the <code>location<\/code> pointer. Then the <em>strstr()<\/em> function is used again.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>Searching:\r\nWas this the face that launch'd a thousand ships, And burnt the topless towers of Ilium.\r\nLooking for: 'the'\r\nString was found at position 10.\r\nString was found at again position 51.<\/code><\/pre>\n<p>Ideally, this code should be set up as a loop that continues to search for matches until the search string is exhausted, i.e., the null character at the end of the string is encountered. Feel free to create such code on your own, which will save you time because such a project will be an Exercise one of these months.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You found the string once, here&#8217;s how to find it again. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=900\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-900","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\/900","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=900"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":935,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions\/935"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}