{"id":6843,"date":"2025-03-01T00:02:25","date_gmt":"2025-03-01T08:02:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6843"},"modified":"2025-03-11T08:54:02","modified_gmt":"2025-03-11T15:54:02","slug":"keep-finding-that-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6843","title":{"rendered":"Keep Finding That String"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>One of the most clever string functions is <em>strstr()<\/em>, which searches for one string within another. Because of its return value &mdash; a <em>char<\/em> pointer &mdash; it&#8217;s possible to call this function multiple times to continue searching through the string.<br \/>\n<!--more--><br \/>\nI first wrote about the <em>strstr()<\/em> function <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=889\">back in 2014<\/a>. I also demonstrated (with lovely illustrations) how to call the function a second time in a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=900\">follow-up post<\/a>. But I left it at that. Now it&#8217;s time to pick up the slack and use the <em>strstr()<\/em> function to find all instances of needles in a haystack.<\/p>\n<blockquote><p>By the way, finding needles in a haystack has nothing to do with sewing needles. The old phrase refers to pine needles. Hay is food for horses. Pine needles are not. A modern comparison would be finding a twig in your spaghetti.<\/p><\/blockquote>\n<p>From that ancient Lesson in 2014, I&#8217;ve created the following code for you to use as a base for this month&#8217;s Exercise:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_03_01-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_03_01-Exercise.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 phrase[] = \"Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?\";\r\n    char find[] = \"the\";\r\n    char *found;\r\n\r\n    puts(phrase);\r\n    found = strstr(phrase,find);\r\n    printf(\"Found '%s' at offset %d.\\n\",\r\n            find,\r\n            (int)(found-phrase)\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code searches for the word <code>\"the\"<\/code> in the <code>phrase[]<\/code> array. It finds it only once, reporting the word and its offset. Here&#8217;s a sample run:<\/p>\n<p><code>Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?<br \/>\nFound 'the' at offset 9.<\/code><\/p>\n<p>The offset is calculated as the difference between the location returned from the <em>strstr()<\/em> function and the base of the <code>phrase[]<\/code> array, <code>found-phrase<\/code>. This value is typecast to an integer for output as the offset in characters.<\/p>\n<p>The code doesn&#8217;t do any error-checking. When the <em>strstr()<\/em> function is unable to find a string, it returns NULL. That&#8217;s beside the point.<\/p>\n<p>Your challenge is to modify the code presented to have the <em>strstr()<\/em> function continue to search the given string. Remember, the value the function returns is an offset (pointer) into the string where the first match is found. It&#8217;s possible to call the function a second time to continue the search. Here&#8217;s output from my solution:<\/p>\n<p><code>Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?<br \/>\nFound 'the' at offset 9.<br \/>\nFound 'the' at offset 60.<\/code><\/p>\n<p>If I modify my solution to search for only the letter combination &#8220;th&#8221;, I see the following output:<\/p>\n<p><code>Was this the face that launch'd a thousand ships, and burnt the topless towers of Ilium?<br \/>\nFound 'th' at offset 4.<br \/>\nFound 'th' at offset 9.<br \/>\nFound 'th' at offset 18.<br \/>\nFound 'th' at offset 34.<br \/>\nFound 'th' at offset 60.<\/code><\/p>\n<p>Please try this exercise on your own before you check out <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6856\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Difficulty: &#9733; &#9733; &#9734; &#9734; One of the most clever string functions is strstr(), which searches for one string within another. Because of its return value &mdash; a char pointer &mdash; it&#8217;s possible to call this function multiple times to &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6843\">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":[3],"tags":[],"class_list":["post-6843","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6843","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=6843"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6843\/revisions"}],"predecessor-version":[{"id":6881,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6843\/revisions\/6881"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}