{"id":39364,"date":"2020-05-16T16:02:18","date_gmt":"2020-05-16T16:02:18","guid":{"rendered":"https:\/\/www.oracletutoring.ca\/blog\/?p=39364"},"modified":"2020-05-16T16:02:19","modified_gmt":"2020-05-16T16:02:19","slug":"comp-sci-c-programming-strpbrk","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/comp-sci-c-programming-strpbrk\/","title":{"rendered":"Comp sci: C programming: strpbrk"},"content":{"rendered":"\n<h2>Self-tutoring about C programming: the tutor mentions how to get the suffix of a string.<\/h2>\n<p>\nPeople who arrive at C from other languages, possibly JavaScript or C# or Python, might notice that string functions seem a little less represented in C. Small wonder: after all, strings don&#8217;t even exist in C; rather, they&#8217;re character arrays. (Yet, ironically, the string functions in C are from &lt;string.h&gt;, its own self-titled library.)\n<\/p>\n<p>\nC string functions seem tuned to get a prefix of a string, but not the suffix. Yet, with a pointer, the suffix is easy from the prefix.\n<\/p>\n<p>Let&#8217;s imagine you have the string<\/p>\n<p>&#8220;She doesn&#8217;t believe in intermittent fasting because&#8230;.&#8221;<\/p>\n<p>\nYou want, from it, to extract everything after &#8220;doesn&#8217;t&#8221;. Here&#8217;s how you might:<\/p>\n<p>\nchar sentence[]=&#8221;She doesn&#8217;t believe in intermittent fasting because&#8230;.&#8221;;<br\/>\nchar *p;<br\/>\np=strpbrk(sentence, &#8220;&apos;&#8221;);\/\/the apostrophe is the target<br\/>\np+=strlen(&#8220;&apos;t &#8220;); \/\/strlen: string length<br\/>\nprintf(&#8220;%s&#8221;,p);<br\/>\n<\/p>\n<p>The output, hopefully:<\/p>\n<p>believe in intermittent fasting because&#8230;.<\/p>\n<p>Source:<\/p>\n<p>Kernighan, Brian and Dennis Ritchie. <em>The C Programming Language<\/em>. New Jersey: Prentice Hall, 1988.<\/p>\nJack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.\n\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Self-tutoring about C programming: the tutor mentions how to get the suffix of a string. People who arrive at C from other languages, possibly JavaScript or C# or Python, might notice that string functions seem a little less represented in &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/comp-sci-c-programming-strpbrk\/\"> <span class=\"screen-reader-text\">Comp sci: C programming: strpbrk<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2995],"tags":[],"class_list":["post-39364","post","type-post","status-publish","format-standard","hentry","category-c-programming"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/39364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=39364"}],"version-history":[{"count":12,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/39364\/revisions"}],"predecessor-version":[{"id":39376,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/39364\/revisions\/39376"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=39364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=39364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=39364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}