{"id":2638,"date":"2017-08-08T00:01:51","date_gmt":"2017-08-08T07:01:51","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2638"},"modified":"2017-08-05T09:56:38","modified_gmt":"2017-08-05T16:56:38","slug":"the-url-encoding-filter-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2638","title":{"rendered":"The URL Encoding Filter &#8211; Solution"},"content":{"rendered":"<p>A URL filter isn&#8217;t that difficult to code, once you know the rules. I&#8217;m sure you can concoct something clever or obfuscated in the C language, but I chose to use a clutch of <em>if<\/em>\/<em>else&nbsp;if<\/em>\/<em>else<\/em> statements to process input and generate output.<br \/>\n<!--more--><br \/>\nFirst, here is the basic filter skeleton:<\/p>\n<pre class=\"screen\">\r\n    int i;\r\n\r\n    while( (i=getchar()) != EOF )\r\n    {\r\n        putchar(i);\r\n    }<\/pre>\n<p>The <em>while<\/em> loop fetches a single character, <code>i<\/code>, from the input stream. The <em>getchar()<\/em> function reads input and the <em>putchar()<\/em> function outputs the character. The <code>EOF<\/code> condition monitors the End Of File marker, which is useful when the filter reads redirected input. All the details are covered in my books.<\/p>\n<p>A straight pipe between input and output is technically a filter, but it&#8217;s not really a computer program because it doesn&#8217;t do anything to alter input. For <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2626\">this month&#8217;s Exercise<\/a>, the task is to convert ASCII code into the percent encoding format, which requires a single statement modification to the basic filter:<\/p>\n<p><code>printf(\"%%%02X\",i);<\/code><\/p>\n<p>The percent sign must be escaped, so two are specified, <code>%%<\/code>. Then comes the output format, which is <code>%02X<\/code>. This placeholder sets output in hexadecimal format, two-digits wide, padded with a leading zero, and with uppercase letters A through F.<\/p>\n<p>Still, the single <em>printf()<\/em> function doesn&#8217;t adhere to the percent encoding format guidelines for HTML 5: Alphanumeric characters are not converted, characters <code>- . _ *<\/code> are left alone, and spaces are converted to <code>+<\/code> signs. To deal with these exceptions, some <em>if else<\/em> action is required. Here is my full solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\nint main()\r\n{\r\n    int i;\r\n\r\n    while( (i=getchar()) != EOF )\r\n    {\r\n        if( i=='-' || i=='.' || i=='_' || i=='*')\r\n            putchar(i);\r\n        else if( i==' ')\r\n            putchar('+');\r\n        else if( isalnum(i) )\r\n            putchar(i);\r\n        else\r\n            printf(\"%%%02X\",i);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The first <em>if<\/em> statement at Line 10 uses multiple <code>||<\/code> (or) conditions to weed out characters <code>- . _ *<\/code>. These characters are output directly at Line 11. All other input slips down to Line 12 for the second comparison.<\/p>\n<p>The <em>else if<\/em> at Line 12 converts the space character to a plus sign.<\/p>\n<p>Next, at Line 14 the <em>else if<\/em> uses the <em>isalnum()<\/em> ctype function to cull out all alphanumeric characters, a to z, A to Z, and 0 to 9. These are output directly at Line 15.<\/p>\n<p>The remaining characters or codes are handled by the <em>else<\/em> at Line 16. The <em>printf()<\/em> statement converts the character to percent encoding.<\/p>\n<p>Another way to handle this operation would be to construct a horrendously long <em>switch case<\/em> structure, where each of the 128 ASCII codes are converted individually or in groups. That code would be fun to write, but it would take more time than the multiple <em>if<\/em> statements in my solution.<\/p>\n<p>If you have a different solution, let me know. You can email me at the address found in the front of my books, or ask for an account here. I must manually sign-up people as the blog software is ripe for phony signups.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A URL filter isn&#8217;t that difficult to code, once you know the rules. I&#8217;m sure you can concoct something clever or obfuscated in the C language, but I chose to use a clutch of if\/else&nbsp;if\/else statements to process input and &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2638\">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-2638","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\/2638","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=2638"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2638\/revisions"}],"predecessor-version":[{"id":2655,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2638\/revisions\/2655"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}