{"id":6856,"date":"2025-03-08T00:02:07","date_gmt":"2025-03-08T08:02:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6856"},"modified":"2025-03-01T11:37:29","modified_gmt":"2025-03-01T19:37:29","slug":"keep-finding-that-string-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6856","title":{"rendered":"Keep Finding That String &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6843\">this month&#8217;s Exercise<\/a> is to repeatedly call the <em>strstr(<\/em>) function, locating all instances of one string (&#8220;needle&#8221;) inside another (&#8220;haystack&#8221;). Obviously your first task, which also helps with the solution, is to perform error checking on the return value from the <em>strstr()<\/em> function.<br \/>\n<!--more--><br \/>\nThe <em>strstr()<\/em> function returns the location (pointer offset) of one string found within another. It returns NULL when the string isn&#8217;t found, which also works when you repeatedly call the function to continue searching.<\/p>\n<p>My solution uses a <em>do-while<\/em> loop to find all instances in string <code>phrase[]<\/code> of string <code>find[]<\/code>. It also uses pointer <code>found<\/code> to assist with the repetition:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_03-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_03-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    if( found==NULL )\r\n        printf(\"String '%s' not found\\n\",find);\r\n    else\r\n    {\r\n        printf(\"Found '%s' at offset %d.\\n\", find, (int)(found-phrase));\r\n        do\r\n        {\r\n            found = strstr(found+1,find);\r\n            if( found != NULL )\r\n                printf(\"Found '%s' at offset %d.\\n\", find, (int)(found-phrase));\r\n        }\r\n        while( found != NULL );\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The first statement to use <em>strstr()<\/em> is outside the <em>do-while<\/em> loop. This location is necessary to initialize variable <code>found<\/code>. Further, if the function returns NULL, the string <code>find[]<\/code> isn&#8217;t located in string <code>phrase[]<\/code>, which means that further searching is pointless.<\/p>\n<p>After the first string <code>find[]<\/code> is located, variable <code>found<\/code> is used inside the <em>do-while<\/em> loop to continue looking for matches. The key here is to start the search at the first character <em>after<\/em> the location stored in variable <code>found<\/code>:<\/p>\n<p><code>found = strstr(found+1,find);<\/code><\/p>\n<p>If you forget the <code>found+1<\/code> expression, the function repeatedly finds the same string.<\/p>\n<p>Upon success, the offset is output and the loop continues until <code>found<\/code> is equal to NULL.<\/p>\n<p>Here is 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.<br \/>\nFound 'the' at offset 60.<\/code><\/p>\n<p>And, as I wrote in the original post, if you modify string <code>find[]<\/code> to represent only &#8220;th&#8221;, the following output is generated:<\/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>I hope that your solution met with success! Remember, multiple approaches are possible for any C language puzzle. Some are more elegant than others, some obfuscated, some down right ugly. If it works, you passed the challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to repeatedly call the strstr() function, locating all instances of one string (&#8220;needle&#8221;) inside another (&#8220;haystack&#8221;). Obviously your first task, which also helps with the solution, is to perform error checking on the &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6856\">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":[5],"tags":[],"class_list":["post-6856","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6856","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=6856"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6856\/revisions"}],"predecessor-version":[{"id":6866,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6856\/revisions\/6866"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}