{"id":605,"date":"2014-03-29T00:01:27","date_gmt":"2014-03-29T08:01:27","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=605"},"modified":"2014-03-22T07:40:10","modified_gmt":"2014-03-22T14:40:10","slug":"direct-string-manipulation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=605","title":{"rendered":"Direct String Manipulation"},"content":{"rendered":"<p>As programmer, you have a choice: You can manipulate information as it&#8217;s sent to output or you can manipulate it in memory and then send the result to output. Depending on what the program does, however, you may not have that luxury. Sometimes you must make modifications in memory, saving them for later.<br \/>\n<!--more--><br \/>\nIn last week&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=600\">Lesson<\/a>, I demonstrated how spaces can be changed to newlines when a string is sent to output. This week&#8217;s Lesson shows you the same thing, but with the string manipulated first, then sent to output. Either way, the object of the game is the same: Hunt down a space and change it to a newline. Here&#8217;s the code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char ob1[] = \"These are not the droids you're looking for.\\n\";\r\n    int x;\r\n\r\n    x = 0;\r\n    while(ob1[x])\r\n    {\r\n        if(ob1[x] == ' ')\r\n            ob1[x] = '\\n';\r\n        x++;\r\n    }\r\n    puts(ob1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This example is actually a tad bit simpler than the example from last week&#8217;s Lesson.<\/p>\n<p>The <em>while<\/em> loop starting at Line 9 plows through the string <code>ob1<\/code>. In the loop, if the character <code>ob1[x]<\/code> is a space, it&#8217;s replaced with a newline (Lines 11 and 12). Otherwise, the value of <code>x<\/code> is incremented to continue working through the string.<\/p>\n<p>The <em>puts()<\/em> function at Line 15 displays the modified results.<\/p>\n<p>To perform the same action in the same way by using pointers, you&#8217;d need to make a few modifications. Primarily, you lose the integer variable <code>x<\/code>, which acts like an index, and replace it with a <em>char<\/em> pointer <code>x<\/code>, which can directly march through the string:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h>&gt;\r\n\r\nint main()\r\n{\r\n    char ob1[] = \"These are not the droids you're looking for.\\n\";\r\n    char *x;\r\n\r\n    x = ob1;\r\n    while(*x)\r\n    {\r\n        if(*x == ' ')\r\n            *x = '\\n';\r\n        x++;\r\n    }\r\n    puts(ob1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Pointer <code>x<\/code> is initialized at Line 8. The <em>while<\/em> loop works pretty much the same, although pointer <code>x<\/code> is manipulated directly instead of using an offset into the <code>ob1<\/code> array (something like <code>*(x+1)<\/code>).<\/p>\n<p>Either way the puzzle is solved, the results are the same: The string is modified in memory, space characters changed to newlines. The string is then output all at once.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Strings can either be manipulated as they&#8217;re output or in memory before they&#8217;re output. This Lesson discusses the before method. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=605\">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-605","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\/605","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=605"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":633,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions\/633"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}