{"id":889,"date":"2014-08-09T00:01:49","date_gmt":"2014-08-09T07:01:49","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=889"},"modified":"2014-08-16T08:00:07","modified_gmt":"2014-08-16T15:00:07","slug":"finding-text-with-strstr","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=889","title":{"rendered":"Finding Text with <em>strstr()<\/em>"},"content":{"rendered":"<p>The programming universe is teaming with search algorithms. In fact, searching for stuff has become so commonplace computer users casually take it for granted. Things weren&#8217;t always that way, but the programming algorithms behind the search remains the same.<br \/>\n<!--more--><br \/>\nThe basic way to find text is to look for it one character at a time. That&#8217;s not very efficient, and I&#8217;m sure Google uses far more complex algorithms (although they might not), but it&#8217;s how I code search routines.<\/p>\n<p>The standard C library tool to locate one string within another is <em>strstr()<\/em>, which I pronounce <em>string-string<\/em>. Here&#8217;s the man page format:<\/p>\n<p><code>char * strstr(const char *s1, const char *s2);<\/code><\/p>\n<p>The <em>strstr()<\/em> function locates the first occurance of string <code>s2<\/code> within string <code>s1<\/code>. That location is returned as a <em>char<\/em> pointer &#8212; the address of <code>s2<\/code> inside <code>s1<\/code>. It&#8217;s not an offset, but a memory location.<\/p>\n<p>Like other string functions, <em>strstr()<\/em> is defined in the <code>string.h<\/code> header file. Here&#8217;s some sample code to chew on:<\/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\";\r\n    char *needle = \"face\";\r\n    char *location;\r\n\r\n    location = strstr(haystack,needle);\r\n    if( location == NULL)\r\n        perror(\"String not found\");\r\n    else\r\n    {\r\n        printf(\"String '%s' was found at position %d in string '%s'.\\n\",\r\n            needle,\r\n            (int)location-(int)haystack+1,\r\n            haystack);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Lines 6 and 7 declare the text and search string, <code>s1<\/code> and <code>s2<\/code>, respectively. Line 8 declares a pointer <code>location<\/code> in which the location of string <code>needle<\/code> in string <code>haystack<\/code> is found.<\/p>\n<p>The <em>strstr()<\/em> function is used at Line 10. The value of <code>location<\/code> is examined at Line 11 and if it&#8217;s <code>NULL<\/code>, then the string wasn&#8217;t found. The program displays an error message and is effectively done at that point.<\/p>\n<p>If a string is found, the <em>printf()<\/em> statement at Lines 15, 16, 17, and 18 is executed. I split up the arguments for <em>printf()<\/em> on three lines for readability.<\/p>\n<p>Line 17 calculates the found string&#8217;s offset. The pointer variables <code>location<\/code> and <code>haystack<\/code> are typecast to integers for the math calculation. The result is incremented by one because the first offset in the string is location 0, which a typical computer user wouldn&#8217;t understand.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>String 'face' was found at position 14 in string 'Was this the face that launch'd a thousand ships'.<\/code><\/p>\n<p>The <em>strstr()<\/em> function has some useful brothers, which I&#8217;ll discuss in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=892\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t find <em>needle<\/em> in <em>haystack<\/em>, but you can find <em>hay<\/em> in <em>haystack<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=889\">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-889","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\/889","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=889"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/889\/revisions"}],"predecessor-version":[{"id":932,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/889\/revisions\/932"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}