{"id":656,"date":"2014-05-01T00:01:08","date_gmt":"2014-05-01T07:01:08","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=656"},"modified":"2014-04-26T07:43:56","modified_gmt":"2014-04-26T14:43:56","slug":"truncate-well-that-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=656","title":{"rendered":"Truncate Well That String"},"content":{"rendered":"<p>Nothing can be more disconcerting than text lopped off before the end of the li<br \/>\n<!--more--><br \/>\nThis truncating issue was more of a problem back in the old text mode days. Monitors (terminals), had a fixed column width. The standard width was usually 80 columns, or 80 characters of text (including spaces) on a line. Even so, good programmers could confirm the terminal&#8217;s column width, which might also be 40 or 64 characters.<\/p>\n<p>Regardless of where the far right column loomed, good programming practice involved formatting text so that it fit neatly within that column width limit. Inelegant programs simply continued their text, slicing words in half and other ugliness. Elegant programmers wrapped their words, ending a line of text at a word boundary. Humans appreciate such efforts.<\/p>\n<p>Your task this month is to write a program that properly truncates a string of text. The truncation must happen at a word boundary, also known as a white space character: a space, tab, or newline.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;ctype.h&gt;\r\n\r\nchar *truncate(char *text, int width);\r\n\r\nint main()\r\n{   \r\n    char sample[] = \"Please lop me off at a word boundary, not in the middle of a word.\";\r\n    int trunc;\r\n\r\n    trunc = 40;\r\n\r\n    printf(\"Truncated at column %d:\\n\",trunc);\r\n    puts(truncate(sample,trunc));\r\n\r\n    return(0);\r\n}   \r\n\r\nchar *truncate(char *text, int width)\r\n{   \r\n}<\/pre>\n<p>Using the skeleton provided above as your starting point, craft the <em>truncate()<\/em> function. Note that the truncating width is presented as a variable, <code>trunc<\/code>. Also &#8212; major hint &#8212; the <code>string.h<\/code> and <code>chtype.h<\/code> header files are included, although your code isn&#8217;t obligated to use either one.<\/p>\n<p>As I admonish every month, please try this exercise on your own before peeking at my solution, linked to below. The C language offers many solutions to the same problem, no one of which is more correct than the other, although ways exist, both elegant and clumsy, to solve any programming puzzle.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/05exercise.c\">Exercise Solution<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anyone can truncate a string, but only programmers who really care know where to slice. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=656\">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-656","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\/656","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=656"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/656\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/656\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}