{"id":4958,"date":"2021-09-08T00:01:08","date_gmt":"2021-09-08T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4958"},"modified":"2021-09-04T09:52:06","modified_gmt":"2021-09-04T16:52:06","slug":"scramble-a-string-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4958","title":{"rendered":"Scramble a String &#8211; Solution"},"content":{"rendered":"<p>For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4946\">this month&#8217;s Exercise<\/a>, my solution to the <em>scramble()<\/em> function requires a second array. You could code a <em>scramble()<\/em> function without using a second array (or storage buffer), swapping individual characters within the string, but I chose not to.<br \/>\n<!--more--><br \/>\nThe <em>scramble()<\/em> function&#8217;s first task is to create a <em>char<\/em> buffer large enough to store the incoming string. Further, I want the buffer to be filled with the null character, <code>\\0<\/code>. The <em>calloc()<\/em> function handles this task, using variable <code>key<\/code> as the <em>char<\/em> pointer to hold the allocated buffer&#8217;s address and <code>len<\/code> as the string size, plus one for the terminating null character:<\/p>\n<p><code>key = calloc( len+1, sizeof(char) );<\/code><\/p>\n<p>Upon success, my solution uses the <code>key<\/code> buffer to store the scrambled string. Here is my complete <em>scramble()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nvoid scramble(char *p,int len)\r\n{\r\n    char *key;\r\n    int x,r;\r\n\r\n    <span class=\"comments\">\/* allocate the temporary string and\r\n       fill will null characters *\/<\/span>\r\n    key = calloc( len+1, sizeof(char) );\r\n    if( key==NULL )\r\n    {\r\n        fprintf(stderr,\"Memory allocation error\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* process the string *\/<\/span>\r\n    x = 0;\r\n    while(x&lt;len)\r\n    {\r\n        r = rand() % len;        <span class=\"comments\">\/* generate random value *\/<\/span>\r\n        if( !*(key+r) )          <span class=\"comments\">\/* look for a null char *\/<\/span>\r\n        {\r\n            *(key+r) = *(p+x);   <span class=\"comments\">\/* assign the character *\/<\/span>\r\n            x++;                 <span class=\"comments\">\/* increment the offset *\/<\/span>\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* update the scrambled string *\/<\/span>\r\n    strcpy( p, key);\r\n}<\/pre>\n<p>Variable <code>x<\/code> acts as an index into the passed string <code>p<\/code>. The <em>while<\/em> loop spins until all characters in <code>p<\/code> are processed.<\/p>\n<p>First, a random value <code>r<\/code> is generated between 0 and the string&#8217;s length, <code>len<\/code>.<\/p>\n<p>Next, a test is done to determine whether a null character exists at position <code>r<\/code> in buffer <code>key<\/code>: <code>if( !*(key+r) )<\/code> This expression is true when the character is null. Otherwise, the <em>while<\/em> loop keeps spinning with additional random values generated.<\/p>\n<p>When a null character is found in <code>key<\/code>, it&#8217;s replaced by the sequential character in <code>p<\/code>, referenced by variable <code>x<\/code>:<\/p>\n<p><code>*(key+r) = *(p+x);<\/code><\/p>\n<p>Variable <code>x<\/code> is then incremented, and the <em>while<\/em> loop continues.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_09-Exercise.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view my full solution, which includes the header files <code>stdio.h<\/code>, <code>stdlib.h<\/code>, <code>string.h<\/code>, and <code>time.h<\/code>. Each of these are required for functions used in the <em>main()<\/em> skeleton presented in the original post, so none of them should surprise you.<\/p>\n<p>This solution reminds me of the Lotto code in my C programming books. It works on a similar basis: using an array to track random items. Though the &#8220;array&#8221; in my <em>scramble()<\/em> function is an allocated buffer, the concept is the same.<\/p>\n<p>I hope your solution meets with success. Especially if you used the character swap method, which I would find a fascinating approach to the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For this month&#8217;s Exercise, my solution to the scramble() function requires a second array. You could code a scramble() function without using a second array (or storage buffer), swapping individual characters within the string, but I chose not to.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4958","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4958","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=4958"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4958\/revisions"}],"predecessor-version":[{"id":4973,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4958\/revisions\/4973"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}