{"id":4946,"date":"2021-09-01T00:01:42","date_gmt":"2021-09-01T07:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4946"},"modified":"2021-09-08T10:43:58","modified_gmt":"2021-09-08T17:43:58","slug":"scramble-a-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4946","title":{"rendered":"Scramble a String"},"content":{"rendered":"<h2>Difficulty: Hard<\/h2>\n<p>Most functions are nice to strings. They manipulate the characters in a useful or clever way, returning an important value or an updated version of the text. But not every string function needs to be so kind, such as the one you write for this Exercise.<br \/>\n<!--more--><br \/>\nIt&#8217;s called the <em>scramble()<\/em> function. It consumes a string and rearranges the characters in random order. Here&#8217;s a sample run:<\/p>\n<p><code>Original: Onomatapoeia<br \/>\nScrambled: epaOiaaonmto<\/code><\/p>\n<p>The <em>scramble()<\/em> function effectively rearranged all the letters in <em>Onomatopoeia<\/em> into <em>epaOiaaonmto<\/em>. This result could be useful to someone somewhere, but you don&#8217;t need to care as you attempt to code the <em>scramble()<\/em> function on your own.<\/p>\n<p>To be kind to you, here is the skeleton you can use to start your coding efforts:<\/p>\n<pre class=\"screen\">\r\nvoid scramble(char *p,int len)\r\n{\r\n}\r\n\r\nint main()\r\n{\r\n    char word[] = \"Onomatapoeia\";\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* scramble the string *\/<\/span>\r\n    printf(\"Original: %s\\n\",word);\r\n    scramble(word,strlen(word));\r\n    printf(\"Scrambled: %s\\n\",word);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>scramble()<\/em> function accepts the string&#8217;s address and its length. Your task is to code the function&#8217;s guts, somehow magically rearranging the characters in a random manner. Remember: The function doesn&#8217;t return a string; the passed string is modified through pointers. And don&#8217;t forget to include the proper header files!<\/p>\n<p>Please try this Exercise on your own before you check out <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4958\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nSetrrm bil acgaS <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4946\">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-4946","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\/4946","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=4946"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4946\/revisions"}],"predecessor-version":[{"id":4979,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4946\/revisions\/4979"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}