{"id":72,"date":"2013-12-01T00:01:09","date_gmt":"2013-12-01T08:01:09","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=72"},"modified":"2013-11-09T09:53:18","modified_gmt":"2013-11-09T17:53:18","slug":"numbers-with-commas","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=72","title":{"rendered":"Numbers With Commas"},"content":{"rendered":"<p>As a programmer, you&#8217;re used to seeing values like <code>1000<\/code> or <code>1234999<\/code>. Your users aren&#8217;t. They prefer to see values presented as <code>1,000<\/code> or <code>1,234,999.01<\/code>. Or, in Europe the format may look like this <code>1.000<\/code> or <code>1.234.999,01<\/code>.<br \/>\n<!--more--><br \/>\nThe task for this exercise is to create a function that properly places the value separators inside a number string. You can choose whether to use US or European formats.<\/p>\n<p>This is not an easy exercise. In fact, it&#8217;s darn difficult, especially if you&#8217;re just starting out in C. Regardless, I hope that you enjoy a challenge, especially if you have lots of time to work on this project.<\/p>\n<p>Like most programming puzzles, this one can be best accomplished by breaking down the problem into chunks.<\/p>\n<p>The first task is to obtain a value and convert it into a string. I would recommend generating a random value. Use the <em>sprintf()<\/em> function to convert the value into a string.<\/p>\n<p>The second task is to work your way through the string to find the positions for the commas. One technique you can use is similar to an example I offer in my books: converting vowels in a sentence to some other character. But in this case, you&#8217;re not converting, you&#8217;re adding a character to the string. So review my vowel-conversion code and modify it so that the character (such as <code>*<\/code>) is inserted into the string.<\/p>\n<p>The third task is to determine where the first comma is located. You could evaluate the string right-to-left, which is easy on your brain: Commas appear after every third digit. In C, however, strings are better dealt with left-to-right. So you need to determine where that first comma appears. As a big hint, that operation involves using the <code>%<\/code> operator on the string&#8217;s overall length.<\/p>\n<p>The final task is to insert the commas after every third character. Again, the <code>%<\/code> operator is used. The technique you use to solve the second task could be employed to insert the commas into the string.<\/p>\n<p>Here is sample output from my solution:<\/p>\n<pre><code>Before value:\t1976768711\r\nWith commas:\t1,976,768,711<\/code><\/pre>\n<p>As always, please attempt the exercise before you peek at my solution. Remember that my solution is merely one way to solve the problem.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/12\/12exercise.c\">Exercise Solution<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For your December puzzle you must craft a function that properly formats a number for your human users. This one ain&#8217;t easy! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=72\">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-72","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\/72","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=72"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/72\/revisions\/413"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}