{"id":26,"date":"2009-01-09T03:14:29","date_gmt":"2009-01-09T03:14:29","guid":{"rendered":"http:\/\/www.sanchitkarve.com\/blog\/?p=26"},"modified":"2009-01-09T03:14:29","modified_gmt":"2009-01-09T03:14:29","slug":"reversing-a-string-recursively","status":"publish","type":"post","link":"https:\/\/www.sanchitkarve.com\/blog\/2009\/01\/09\/reversing-a-string-recursively\/","title":{"rendered":"Reversing a String recursively"},"content":{"rendered":"<p>When you\u2019re asked to reverse a string, you\u2019ll mostly use the strrev() function or write your own boring implementation using loops.<br \/>\nEver tried it recursively?<\/p>\n<p>Have a look at this:<br \/>\n#include &lt;iostream&gt;<\/p>\n<pre lang=\"cpp\">using namespace std;\n\nvoid ret_str(char* s)\n{\n\nif(*s != \u201d)\nret_str(s+1);<\/pre>\n<p>cout &lt;&lt; *s;<\/p>\n<pre lang=\"cpp\">\n}\n\nint main()\n{\nret_str(\u201dborn2c0de\u201d);\nreturn 0;\n}<\/pre>\n<p>Isn\u2019t that some neat piece of code? All we have to do is push the next character to the stack, so when the stack is popped, the characters come out in reverse order.<\/p>\n<p>However this functions isn&#8217;t efficient, it\u2019s horribly slow and sluggish and larger strings will result in overloading the stack so you&#8217;re better off using the functions which use loops internally.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you\u2019re asked to reverse a string, you\u2019ll mostly use the strrev() function or write your own boring implementation using loops. Ever tried it recursively? Have a look at this: #include &lt;iostream&gt; using namespace std; void ret_str(char* s) { if(*s != \u201d) ret_str(s+1); cout &lt;&lt; *s; } int main() { ret_str(\u201dborn2c0de\u201d); return 0; } Isn\u2019t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[21,56,61],"_links":{"self":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/26"}],"collection":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":0,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}