{"id":383,"date":"2013-11-09T00:01:41","date_gmt":"2013-11-09T08:01:41","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=383"},"modified":"2013-11-02T07:56:10","modified_gmt":"2013-11-02T15:56:10","slug":"duck-duck-goose-and-moduluse","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=383","title":{"rendered":"Duck-Duck-Goose and Moduluse"},"content":{"rendered":"<p>There is no such word as <em>moduluse<\/em>, but the C language does feature the modulus operator, <code>%<\/code>. You can employ that operator do count off intervals, which allows you to manipulate information in a consistent and interesting way.<br \/>\n<!--more--><br \/>\nIn my books, I&#8217;ve provided text-manipulation examples that replace specific characters in a string with an <code>*<\/code> or <code>@<\/code> or some other character. That&#8217;s an easy substitution, but it&#8217;s based on the character itself. You can use the modulus operator to replace characters at predictable intervals in a string in much the same manner.<\/p>\n<p>For a moment, forget about why such a thing is necessary: It&#8217;s the learning process that&#8217;s important!<\/p>\n<p>The following code takes a string of text and replaces every third character with an underscore.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *string = \"When in the Course of human events\";\r\n    int count = 1;\r\n\r\n    while(*string)\r\n    {\r\n        if(count%3)\r\n            putchar(*string);\r\n        else\r\n            putchar('_');\r\n        count++;\r\n        string++;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>count<\/code> variable starts at 1 at Line 6. If it starts with 0 then the first character of the string would be turned into an underscore, which isn&#8217;t exactly what I wanted.<\/p>\n<p>The meat of the code is found in the <em>if<\/em> decision at Line 10. The result of <code>count%3<\/code> is true for those values not equally divisible by 3, but false for values divisible by three. In other words, every third item. In this case, the <em>else<\/em> part of the structure handles that third item, displaying the underscore.<\/p>\n<p>Here is sample output:<\/p>\n<pre><code>Wh_n _n _he_Co_rs_ o_ h_ma_ e_en_s<\/code><\/pre>\n<p>If you&#8217;d rather <em>insert<\/em> the underscore instead of replacing, then you need to modify the code. Try that on your own to see if you fully understand the concept. When you&#8217;re ready, you can review my solution by <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/11\/1109b.c\">clicking here<\/a>.<\/p>\n<p>Here is the sample output for my solution:<\/p>\n<pre><code>Whe_n i_n t_he _Cou_rse_ of_ hu_man_ ev_ent_s<\/code><\/pre>\n<p>Unlike the first sample output, all of the text from the original string is displayed. After every third character, however, an underscore is inserted. (Well, actually, the underscore is output; the original string is unmodified.)<\/p>\n<p>The purpose of this lesson will become obvious when I unveil the horrible, arduous exercise for December. Stay tuned for that!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The modulus operator can be used to slice and dice information, such as a string of text, in a consistent and predictable way. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=383\">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-383","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\/383","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=383"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":396,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions\/396"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}