{"id":3039,"date":"2018-04-08T00:01:25","date_gmt":"2018-04-08T07:01:25","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3039"},"modified":"2018-04-07T10:50:21","modified_gmt":"2018-04-07T17:50:21","slug":"highlight-a-chunk-of-text-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3039","title":{"rendered":"Highlight a Chunk of Text &#8211; Solution"},"content":{"rendered":"<p>Your task for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=3022\">this month&#8217;s Exercise<\/a> is to code a text-processing routine that interprets the ^ character as a toggle for all-caps output. This challenge can be difficult, depending on how you interpret the toggle.<br \/>\n<!--more--><br \/>\nFor my first solution, I used an <em>if-else<\/em> decision structure. One part processed uppercase text and the other processed normal text based on when the ^ character is encountered. This solution didn&#8217;t work, especially when two ^ characters were encountered in a row. To handle that case, more complexity and ugliness was required. So, I started over.<\/p>\n<p>For my second attempt, I decided to make the ^ character activate a toggle. When the ^ character is encountered, the value of the <code>uc<\/code> variable is set to 1. When the ^ character appears again, <code>uc<\/code> is reset to 0:<\/p>\n<p> <code>uc = uc ? 0 : 1;<\/code><\/p>\n<p>Variable <code>uc<\/code> is initialized to 0. (I used an <em>int<\/em> variable instead of a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2953\"><em>_Bool<\/em><\/a>, though a <em>_Bool<\/em> would also work.) The above ternary function toggles the value of <code>uc<\/code> between 0 and 1: If it&#8217;s already 0, <code>uc<\/code> is set to 1, otherwise the value is reset to 0.<\/p>\n<p>As the code processes the text, a second ternary function converts characters to uppercase based on the value of <code>uc<\/code>:<\/p>\n<p><code>*o = uc ? toupper(*i) : *i;<\/code><\/p>\n<p>Pointer variable <code>o<\/code> references the output string, pointer variable <code>i<\/code> references the input string. When the value of <code>uc<\/code> is 1 (TRUE), the <em>toupper()<\/em> function converts the input character to upper case. Otherwise, the input character slides through unaffected.<\/p>\n<p>I could have used an <em>if-else<\/em> structure in both instances instead of the ternary function, which is more readable. I could have also used array notation to work with the input and output strings, but I didn&#8217;t because understanding pointers is important.<\/p>\n<p>Here is the full code for my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\n#define SIZE 255\r\n\r\nint main()\r\n{\r\n    char input[SIZE];\r\n     char output[SIZE];\r\n    char *i,*o;\r\n    int uc;\r\n\r\n<span class=\"comments\">\/* Initialize variables *\/<\/span>\r\n    i = input;\r\n    o = output;\r\n    uc = 0;\r\n\r\n<span class=\"comments\">\/* use fgets() to read and restrict input *\/<\/span>\r\n     printf(\"Input: \");\r\n     fgets(input,SIZE,stdin);\r\n\r\n<span class=\"comments\">\/* process the line *\/<\/span>\r\n    while( *i!='\\n')\r\n    {\r\n        if( *i =='^')\r\n        {\r\n            uc = uc ? 0 : 1;    <span class=\"comments\">\/* toggle on\/off *\/<\/span>\r\n            i++;                <span class=\"comments\">\/* skip ^ char *\/<\/span>\r\n        }\r\n        else\r\n        {\r\n            *o = uc ? toupper(*i) : *i;\r\n            o++;\r\n            i++;\r\n        }\r\n        if( *i=='\\0')\r\n            break;\r\n    }\r\n\r\n<span class=\"comments\">\/* display result *\/<\/span>\r\n    printf(\"Output: %s\\n\",output);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>while<\/em> loop at Line 23 processes the string until the newline is encountered. Remember that the <em>fgets()<\/em> function retains the newline.<\/p>\n<p>The <code>i++<\/code> statement at Line 28 increments the input string beyond the ^ character. And in the <em>else<\/em> statements, both <code>o<\/code> and <code>i<\/code> are incremented to continue working through the strings. The size of the <code>o<\/code> buffer can be the same as the <code>i<\/code> buffer as it can&#8217;t be larger due to skipping the ^ character in the <code>o<\/code> string.<\/p>\n<p>The <em>if<\/em> test at Line 36 checks for the null character, <code>\\0<\/code>. This test is necessary should the input string length be greater than the buffer size. When that happens, the input string terminates with a null character, not a newline.<\/p>\n<p>My solution is only one approach. If yours processes the strings as shown in the <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=3022\">examples<\/a>, great!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your task for this month&#8217;s Exercise is to code a text-processing routine that interprets the ^ character as a toggle for all-caps output. This challenge can be difficult, depending on how you interpret the toggle.<\/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-3039","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\/3039","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=3039"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3039\/revisions"}],"predecessor-version":[{"id":3059,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3039\/revisions\/3059"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}