{"id":4986,"date":"2021-10-02T00:01:46","date_gmt":"2021-10-02T07:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4986"},"modified":"2021-09-25T09:37:09","modified_gmt":"2021-09-25T16:37:09","slug":"frying-a-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4986","title":{"rendered":"Frying a String"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4946\">September&#8217;s Exercise<\/a> is to scramble a string, jumbling its characters in a random pattern. I figured it&#8217;s a fun exercise, not anything useless beyond curiosity. Yet such a function exists for the GNU C library, <em>strfry()<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>strfry()<\/em> function isn&#8217;t part of the standard C library. In fact, you must contort your code a bit to rummage through the GNU C library to access it. Such digital gymnastics means that only certain environments have access to the GNU C library. In my cursory examination of the <em>strfry()<\/em> function, I&#8217;ve discovered that it&#8217;s available primarily in Linux.<\/p>\n<p>To quickly check for the <em>strfry()<\/em> function&#8217;s availability, use the command <strong>man strfry<\/strong> in a terminal window. If no entry is found, bummer. Otherwise, you see this format:<\/p>\n<p><code>char *strfry(char *string);<\/code><\/p>\n<p>Unlike my <em>scramble()<\/em> function which modified the original string, the <em>strfry()<\/em> function returns a freshly scrambled string, leaving the original string as-is. The function requires the <code>string.h<\/code> header file &mdash; but more importantly, it requires the <code>_GNU_SOURCE<\/code> defined constant declared at the tippy-top of the source code file:<\/p>\n<p><code>#define _GNU_SOURCE<\/code><\/p>\n<p>It&#8217;s this directive that activates the GNU C library functions, making them available where otherwise they lie dormant. This <code>#define<\/code> is known as a <em>feature test macro<\/em>. It exposes certain functions in header files, which means this preprocessor directive must be declared first, at the tippy-top of the source code file:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_10_02-Lesson.c\" rel=\"noopener\" target=\"_blank\">2021_10_02-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#define _GNU_SOURCE\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n\r\nint main()\r\n{\r\n    char s[] = \"Hello there, handsome devil!\";\r\n\r\n    printf(\"Before: %s\\n\",s);\r\n    printf(\"After: %s\\n\",strfry(s));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 1 is the <code>_GNU_SOURCE<\/code> feature test macro. It exposes the custom GNU library functions in the <code>stdio.h<\/code> and <code>string.h<\/code> header files. Specifically, it rouses the <em>strfry()<\/em> function in the <em>printf()<\/em> statement at Line 11. This function&#8217;s return value is immediately swallowed by the <em>printf()<\/em> function&#8217;s <code>%s<\/code> placeholder, outputting the scrambled string:<\/p>\n<p><code>Before: Hello there, handsome devil!<br \/>\nAfter: ll emete ,dheno!vH aidsoerhl<\/code><\/p>\n<p>I&#8217;m unaware of the function&#8217;s internals, whether it uses the randomized method I chose for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4958\">my solution to last month&#8217;s Exercise<\/a> or whether it uses the character-swap method demonstrated by Chris Webb. My guess is that it uses the character-swap method.<\/p>\n<p>Obviously, <em>strfry()<\/em> is an oddball function. I suppose it could end up in other libraries, though I don&#8217;t see why. While it&#8217;s fun and interesting, the desire to scramble a string doesn&#8217;t seem to be high on any programmer&#8217;s priority list. And, as always, if you need a function that&#8217;s not in any library, you can always code it yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The GNU C library features a string scrambling function, <em>strfry()<\/em>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4986\">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-4986","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\/4986","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=4986"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4986\/revisions"}],"predecessor-version":[{"id":5001,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4986\/revisions\/5001"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}