{"id":3568,"date":"2019-04-20T00:01:55","date_gmt":"2019-04-20T07:01:55","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3568"},"modified":"2019-04-13T11:02:03","modified_gmt":"2019-04-13T18:02:03","slug":"find-the-vowel","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3568","title":{"rendered":"Find the Vowel"},"content":{"rendered":"<p>I admit that sometimes I need to see someone else&#8217;s code before a formerly obscure function becomes useful to me. A case in point is the <em>strchr()<\/em> function, which I rarely use.<br \/>\n<!--more--><br \/>\nThe <em>strchr()<\/em> function scans for the first instance of a character in a string. Prototyped in the <code>string.h<\/code> header, here is the format:<\/p>\n<p><code>char * strchr( const char s*, int c);<\/code><\/p>\n<p><code>s<\/code> is a string and <code>c<\/code> is the character. If character <code>c<\/code> is found in string <code>s<\/code>, its location (pointer) is returned, otherwise the NULL pointer is returned. This is the man page definition and it&#8217;s the way I&#8217;ve always looked at the function, which I&#8217;ve just discovered cuts the function short.<\/p>\n<p>Another way to look at <em>strchr()<\/em> is that it confirms when character <code>c<\/code> exists in string <code>s<\/code>. This reverse look at the function makes it more interesting to me. I saw this approach used as a solution to a problem: How to determine whether the first character of a string is a vowel?<\/p>\n<p>Here&#8217;s the function:<\/p>\n<p><code>if( strchr(\"aeiouAEIOU\",text[0]) )<\/code><\/p>\n<p>The first character of the string is represented as <code>text[0]<\/code>. The comparison string is the collection of all vowels, both lower and upper case. If the character at the start of the string matches any character in the vowel string, a pointer is returned. It&#8217;s not saved, but the pointer itself sets the result of the <em>if<\/em> comparison to true. Otherwise the <code>NULL<\/code> pointer sets the result to false.<\/p>\n<p>I had never considered using the <em>strchr()<\/em> function in this backwards manner. It presented a delicious and efficient solution to what would otherwise be a complex testing algorithm. Here&#8217;s the test code I wrote:<\/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 text[] = \"abcdef\";\r\n\r\n    if( strchr(\"aeiouAEIOU\",text[0] ) )\r\n        printf(\"'%s' starts with a vowel\\n\",text);\r\n    else\r\n        printf(\"'%s' does not start with a vowel\\n\",text);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>And the output:<\/p>\n<p><code>'abcdef' starts with a vowel<\/code><\/p>\n<p>If I modify the <code>text[]<\/code> string so that it starts with a Z, the output changes to:<\/p>\n<p><code>'Zabcdef' does not start with a vowel<\/code><\/p>\n<p>The <em>strchr()<\/em> function has a companion <em>strrchr()<\/em>, where the extra <em>r<\/em> stands for reverse. In this function, the pointer is returned for the final occurrence or character <code>c<\/code> in string <code>s<\/code>. I can&#8217;t quite think of a way to transform that function into something clever, so I&#8217;ll wait until I see someone else use it that way then steal it and post it here.<\/p>\n<p>By the way, character <code>c<\/code> can be the null character, <code>\\0<\/code>. When specified, the pointer returned references the null character at the end of string <code>s<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another way to look at the <em>strchr()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3568\">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-3568","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\/3568","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=3568"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3568\/revisions"}],"predecessor-version":[{"id":3570,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3568\/revisions\/3570"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}