{"id":3612,"date":"2019-06-01T00:01:22","date_gmt":"2019-06-01T07:01:22","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3612"},"modified":"2019-05-25T09:50:06","modified_gmt":"2019-05-25T16:50:06","slug":"slicing-words-from-a-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3612","title":{"rendered":"Slicing Words from a String"},"content":{"rendered":"<p>The fall-through effect of the <em>switch-case<\/em> structure can be extremely useful, as demonstrated in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3607\">last week&#8217;s Lesson<\/a>. For slicing words from a string, you can easily match multiple word separators, saving you from coding complex statements or weirdo <em>if-else<\/em> structures. But a problem arises when multiple word separators appear.<br \/>\n<!--more--><br \/>\nThe issue is to know when the separators stop and a new word starts. To efficiently resolve the problem, you can use a ctype function, <em>isalpha()<\/em>, which is what I did in the following code.<\/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    char string[] = \"Hello there strange, little planet\\n\";\r\n    int x = 0;\r\n\r\n    while( string[x] )\r\n    {\r\n        switch( string[x] )\r\n        {\r\n            case ' ':\r\n            case '.':\r\n            case ',':\r\n            case '!':\r\n            case '?':\r\n            case ';':\r\n            case ':':\r\n            case '\\n':\r\n                putchar('\\n');\r\n                if( string[x+1] == '\\0' )\r\n                    break;\r\n                if( !isalpha(string[x+1]) )\r\n                    x++;\r\n                break;\r\n            default:\r\n                putchar( string[x] );\r\n        }\r\n        x++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>After the list of <em>case<\/em> separator character comparisons, the set of statements starting at Line 22 process the next several characters in the string.<\/p>\n<p>First, the newline is output at Line 21.<\/p>\n<p>Second, the code checks the next character in the string, <code>string[x+1]<\/code>, to confirm that it&#8217;s not the null character, marking the end of the string. If it is, the <em>break<\/em> statement exits the <em>switch-case<\/em> structure.<\/p>\n<p>Finally, the <em>isalpha()<\/em> test at Line 24 checks the next character. If the character isn&#8217;t a letter of the alphabet, variable <code>x<\/code> is incremented. This solution skips over two separator characters in a row, such as <code>\", \"<\/code> (comma space) or <code>\". \"<\/code> (period space).<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Hello<br \/>\nthere<br \/>\nstrange<br \/>\nlittle<br \/>\nplanet<\/code><\/p>\n<p>And, naturally, this code isn&#8217;t without its problems.<\/p>\n<p>If you change the string to <code>\"Hello there, strange... little planet\\n\"<\/code>, here&#8217;s the output:<\/p>\n<p><code>Hello<br \/>\nthere<br \/>\nstrange<\/p>\n<p>little<br \/>\nplanet<\/code><\/p>\n<p>The code cannot process multiple word separators; it&#8217;s stuck looking at only the next character, <code>string[x+1]<\/code>. A fix isn&#8217;t that difficult, as the following modification demonstrates.<\/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    char string[] = \"Hello there, strange... little planet\\n\";\r\n    int x,y;\r\n\r\n    x = 0;\r\n    while( string[x] )\r\n    {\r\n        switch( string[x] )\r\n        {\r\n            case ' ':\r\n            case '.':\r\n            case ',':\r\n            case '!':\r\n            case '?':\r\n            case ';':\r\n            case ':':\r\n            case '\\n':\r\n                putchar('\\n');\r\n                y = 1;\r\n                while(string[x+y])\r\n                {\r\n                    if( !isalpha(string[x+y]) )\r\n                        y++;\r\n                    else\r\n                        break;\r\n                }\r\n                x+=y;\r\n            default:\r\n                putchar( string[x] );\r\n        }\r\n        x++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>A new variable is needed, <code>y<\/code>. It&#8217;s initialized at Line 23 then used in the <em>while<\/em> loop starting at Line 24.<\/p>\n<p>Within the <em>while<\/em> loop, subsequent characters in the string are applied to the <em>isalpha()<\/em> test:<\/p>\n<p><code>if( !isalpha(string[x+y]) )<\/code><\/p>\n<p>If this test passes, meaning the next character in the string isn&#8217;t a letter of the alphabet, variable <code>y<\/code> is incremented and the loop repeats. Otherwise, the <em>else<\/em> condition takes over and the loop breaks.<\/p>\n<p>When an alphabetic letter is found, the value of variable <code>y<\/code> is added to the value of variable <code>x<\/code> at Line 31. The rest of the string is then processed.<\/p>\n<p>Here&#8217;s sample output:<\/p>\n<p><code>Hello<br \/>\nthere<br \/>\nstrange<br \/>\nlittle<br \/>\nplanet<\/code><\/p>\n<p>Hang on to the second example from this Lesson because it&#8217;s used as the base for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3616\">this month&#8217;s Exercise<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A bit of finesse is required to pluck a word from a string when the word is followed by multiple separators. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3612\">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-3612","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\/3612","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=3612"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3612\/revisions"}],"predecessor-version":[{"id":3629,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3612\/revisions\/3629"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}