{"id":6019,"date":"2023-09-08T00:01:47","date_gmt":"2023-09-08T07:01:47","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6019"},"modified":"2023-09-02T11:04:01","modified_gmt":"2023-09-02T18:04:01","slug":"finding-characters-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6019","title":{"rendered":"Finding Characters &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6009\">this month&#8217;s Exercise<\/a> is to write the <em>charcount()<\/em> function, which returns the number of characters in a string. The character matches exactly, so don&#8217;t worry about checking uppercase and lowercase differences.<br \/>\n<!--more--><br \/>\nMy solution involves using a <em>while<\/em> loop to move a pointer through the string. An <em>if<\/em> comparison checks for the given character, tallying the result in variable <code>total<\/code>. Here is the full code, with my <em>charcount()<\/em> function up top:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_09-Exercise.c\" rel=\"noopener\" target=\"_blank\">2023_09-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint charcount(char *s,char c)\r\n{\r\n    int total = 0;\r\n\r\n    while(*s)\r\n    {\r\n        if( *s==c )\r\n            total++;\r\n        s++;\r\n    }\r\n\r\n    return(total);\r\n}\r\n\r\nint main()\r\n{\r\n    const int size = 64;\r\n    char buffer[size];\r\n    char c,t;\r\n\r\n    printf(\"Enter a string: \");\r\n    fgets(buffer,size,stdin);\r\n    printf(\"Enter a character to find: \");\r\n    scanf(\"%c\",&amp;c);\r\n\r\n    t = charcount(buffer,c);\r\n    printf(\"There are %d letters '%c' in: %s\",\r\n            t,\r\n            c,\r\n            buffer\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Pointer <code>s<\/code> represents the string passed. The <em>while<\/em> loop processes the entire string: <code>while(*s)<\/code> with the <code>s<\/code> pointer address incremented within the loop: <code>s++<\/code> It&#8217;s okay to modify this pointer variable within the <em>charcount()<\/em> function as this change doesn&#8217;t affect the pointer in the <em>main()<\/em> function.<\/p>\n<p>Variable <code>total<\/code> is initialized to zero: <code>int total = 0;<\/code> It&#8217;s incremented only when a match is found inside the string. When no characters match, the preset value zero is returned.<\/p>\n<p>Here is a sample run of my solution:<\/p>\n<p><code>Enter a string: Peter Piper picked a peck of pickled peppers<br \/>\nEnter a character to find: p<br \/>\nThere are 7 letters 'p' in: Peter Piper picked a peck of pickled peppers<\/code><\/p>\n<p>I hope your solution met with success. Remember, mine isn&#8217;t the only approach to solving the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to write the charcount() function, which returns the number of characters in a string. The character matches exactly, so don&#8217;t worry about checking uppercase and lowercase differences.<\/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-6019","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\/6019","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=6019"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6019\/revisions"}],"predecessor-version":[{"id":6026,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6019\/revisions\/6026"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}