{"id":7479,"date":"2026-04-01T00:01:24","date_gmt":"2026-04-01T07:01:24","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7479"},"modified":"2026-04-10T09:02:20","modified_gmt":"2026-04-10T16:02:20","slug":"a-little-bit-off-the-sides","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7479","title":{"rendered":"A Little Bit Off the Sides"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9733; &#9734;<\/h2>\n<p>As you might already know, the C language lacks plenty o&#8217; functions readily available in other programming languages. Many of these functions, or &#8220;methods,&#8221; deal with strings. Though C includes a few basic string functions in its library, the C Lords have determined that when you need another such function, you must code it yourself.<br \/>\n<!--more--><br \/>\nThe topic for this month&#8217;s Exercise is to craft a <em>trim()<\/em> function, which is one of those capabilities present in other programming languages but not directly in C. What this function does is to remove whitespace characters from both ends of a string.<\/p>\n<p>As a reminder, whitespace characters include space, newline, and tab. The POSIX standard also adds form-feed (<code>\\f<\/code>), carriage return (<code>\\r<\/code>), and vertical tab (<code>\\v<\/code>).<\/p>\n<p>Below is a code skeleton to help get you started. This code runs, but it&#8217;s missing the <em>trim()<\/em> function and the statements to call the function and output the trimmed strings.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_04_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_04_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/*\r\n   put the trim() function here\r\n *\/<\/span>\r\n\r\nint main()\r\n{\r\n    char *sample[] = {\r\n        \"   one   \",\r\n        \"\\ttwo\\n\",\r\n        \"\",\r\n        \"    \",\r\n        \"a\",\r\n        \" x \",\r\n        NULL,\r\n        \" \\t three \\n\",\r\n        \"four\",\r\n        \"  five\",\r\n        \"six    \",\r\n        \" seven eight \"\r\n    };\r\n    int size,x;\r\n\r\n    <span class=\"comments\">\/* obtain array size *\/<\/span>\r\n    size = sizeof(sample)\/sizeof(sample[0]);\r\n\r\n    <span class=\"comments\">\/* output trimmed strings *\/<\/span>\r\n    for( x=0; x&lt;size; x++ )\r\n    {\r\n        <span class=\"comments\">\/* output string\r\n           and trimmed string *\/<\/span>\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The skeleton includes an array <code>sample[]<\/code> that contains a slew of sample strings to trim. The strings represent a variety of possibilities such as an empty string, a string with only spaces, and NULL. These items are included because your <em>trim()<\/em> function solution must account for everything!<\/p>\n<p>You can see that I don&#8217;t prototype the <em>trim(<\/em>) function, so its behavior is truly at your discretion.<\/p>\n<p>Here is sample output from my solution:<\/p>\n<pre>'   one   ' => 'one'\r\n'       two\r\n' => 'two'\r\n'' => ''\r\n'    ' => ''\r\n'a' => 'a'\r\n' x ' => 'x'\r\n'(null)' => Bad string\r\n'        three\r\n' => 'three'\r\n'four' => 'four'\r\n'  five' => 'five'\r\n'six    ' => 'six'\r\n' seven eight ' => 'seven eight'<\/pre>\n<p>I can think of multiple ways to code a <em>trim()<\/em> function, but don&#8217;t overthink things! As a suggestion, I recommend tackling the problem one step at a time.<\/p>\n<p>Please try this exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7493\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time to test your skills and code a common string function not available in the standard C library. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7479\">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":[3],"tags":[],"class_list":["post-7479","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7479","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=7479"}],"version-history":[{"count":12,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7479\/revisions"}],"predecessor-version":[{"id":7544,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7479\/revisions\/7544"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}