{"id":6009,"date":"2023-09-01T00:01:47","date_gmt":"2023-09-01T07:01:47","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6009"},"modified":"2024-03-27T16:15:08","modified_gmt":"2024-03-27T23:15:08","slug":"finding-characters","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6009","title":{"rendered":"Finding Characters"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>When I was working on <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5967\">last month&#8217;s Exercise<\/a>, I scoured the C library for a function that returned the number of specific characters found in a string. The closest I could find was <em>strchr()<\/em>, which returns the location of a given character in a string. You can set this function in a loop to find subsequent characters, but what I needed was a tally, a total count of all matching characters in the string.<br \/>\n<!--more--><br \/>\nFor example, given the string <code>\"She sells seashells by the seashore\"<\/code>, how many letters <code>s<\/code> (lowercase only) are found in the string?<\/p>\n<p>Don&#8217;t use your eyeballs to count! No, your task for this month&#8217;s Exercise is to code a function <em>charcount()<\/em>. It accepts two arguments, a string to scan and the character to count. The function returns the number of characters found.<\/p>\n<p>You can use this sample code to get you started, which is missing the <em>charcount()<\/em> function details, of course:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_09_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_09_01-Lesson.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}\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>Here is a sample run from my solution:<\/p>\n<p><code>Enter a string: She sells seashells by the seashore<br \/>\nEnter a character to find: s<br \/>\nThere are 7 letters 's' in: She sells seashells by the seashore<\/code><\/p>\n<p>Your task is to complete the <em>charcount()<\/em> function. Please try this Exercise on your own before you check out <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6019\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many characters <em>c<\/em> are found in a string? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6009\">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":[3],"tags":[],"class_list":["post-6009","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6009","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=6009"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6009\/revisions"}],"predecessor-version":[{"id":6332,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6009\/revisions\/6332"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}