{"id":3218,"date":"2018-08-08T00:01:31","date_gmt":"2018-08-08T07:01:31","guid":{"rendered":"https:\/\/c-for-dummies.com\/?p=3218"},"modified":"2018-08-04T11:23:23","modified_gmt":"2018-08-04T18:23:23","slug":"duplicated-letters-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3218","title":{"rendered":"Duplicated Letters &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3207\">this month&#8217;s Exercise<\/a> is to write code that counts repeating letters in a string. For example, the string &#8220;Hello, this is a test&#8221; repeats the letters T and S three times, letters E, H, I, and L twice, and the letters A and O appear only once.<br \/>\n<!--more--><br \/>\nFor my solution, I created two arrays, <em>char<\/em> <code>input[]<\/code> and <em>int<\/em> <code>alphabet[]<\/code>:<\/p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;char input[64],alphabet[26];<\/code><\/p>\n<p>The <code>input[]<\/code> array holds the string input and <code>alphabet[]<\/code> keeps track of the letters that repeat. It&#8217;s initialized to all zeros, which represent false values for each letter of the alphabet:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* initialize alphabet array *\/<\/span>\r\nfor(x=0;x&lt;26;x++)\r\n    alphabet[x] = 0;<\/pre>\n<p>After text is input, a <em>while<\/em> loop scans each character in the <code>input[]<\/code> array, the string input:<\/p>\n<pre class=\"screen\">\r\nx=max=0;\r\nwhile(input[x])\r\n{\r\n    ch = input[x];\r\n    <span class=\"comments\">\/* skip non-alphabet characters *\/<\/span>\r\n    if(isalpha(ch))\r\n    {\r\n        <span class=\"comments\">\/* increment character count *\/<\/span>\r\n        alphabet[toupper(ch)-'A']++;\r\n        <span class=\"comments\">\/* track largest count value *\/<\/span>\r\n        if( alphabet[toupper(ch)-'A'] &gt; max )\r\n            max = alphabet[toupper(ch)-'A'];\r\n    }\r\n    x++;\r\n}<\/pre>\n<p>The <em>char<\/em> variable <code>ch<\/code> grabs a character from the <code>input[]<\/code> array; <em>int<\/em> variable <code>x<\/code> as an index. The <code>isalpha(ch)<\/code> test culls out only letters of the alphabet. When a letter is found, it&#8217;s offset within the index is incremented: <code>alphabet[toupper(ch)-'A']++<\/code>.<\/p>\n<p>Variable <code>max<\/code> tracks the maximum number of letters repeated. So if U appears 6 times, <code>max<\/code> holds the value six. Where <code>max<\/code> is used is in a nested <em>for<\/em> loop structure that follows:<\/p>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/* process results *\/<\/span>\r\nif(max&gt;1)\r\n{\r\n    for(x=max;x&gt;0;x--)\r\n    {\r\n        printf(\"Letter count %d:\",x);\r\n        for(y=0;y&lt;26;y++)\r\n            if(alphabet[y]==x)\r\n                printf(\" %c\",y+'A');\r\n        putchar('\\n');\r\n    }\r\n}\r\nelse\r\n{\r\n    puts(\"String is unique: No two letters repeat.\");\r\n}<\/pre>\n<p>When <code>max<\/code> isn&#8217;t greater than 1, the <em>if<\/em> test fails and it&#8217;s assumed that no two letters repeat. Otherwise, the outer <em>for<\/em> loop scans each letter count value from <code>max<\/code> down to 1. The inner loop displays the repeated characters.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<p><code>Enter text: Unique words<br \/>\nLetter count 2: U<br \/>\nLetter count 1: D E I N O Q R S W<\/code><\/p>\n<p>And:<\/p>\n<p><code>Enter text: Ugly Ed is an ox<br \/>\nString is unique: No two letters repeat.<\/code><\/p>\n<p>And:<\/p>\n<p><code>Enter text: My solution is nothing fancy.<br \/>\nLetter count 4: N<br \/>\nLetter count 3: I O<br \/>\nLetter count 2: S T Y<br \/>\nLetter count 1: A C F G H L M U<\/code><\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/08\/08exercise-b.c\">Click here<\/a> to view the full code for my solution. I also wrote a simpler version of this solution, one that displays repeated letters as they&#8217;re encountered. You can view this solution <a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/08\/08exercise-a.c\">here<\/a>.<\/p>\n<p>I hope that you&#8217;ve devised some interesting solutions to the problem. Remember, no one perfect solution exists in C.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to write code that counts repeating letters in a string. For example, the string &#8220;Hello, this is a test&#8221; repeats the letters T and S three times, letters E, H, I, and L &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3218\">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":[5],"tags":[],"class_list":["post-3218","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\/3218","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=3218"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3218\/revisions"}],"predecessor-version":[{"id":3237,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3218\/revisions\/3237"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}