{"id":25467,"date":"2017-10-15T18:45:32","date_gmt":"2017-10-15T13:15:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25467"},"modified":"2017-10-15T18:45:32","modified_gmt":"2017-10-15T13:15:32","slug":"c-programming-print-possible-strings-can-made-placing-spaces-2-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-print-possible-strings-can-made-placing-spaces-2-2\/","title":{"rendered":"C++ Programming Print all possible strings that can be made by placing spaces"},"content":{"rendered":"<p>Given a string you need to print all possible strings that can be made by placing spaces (zero or one) in between them.<\/p>\n<pre>Input:  str[] = \"ABC\"\r\nOutput: ABC\r\n        AB C\r\n        A BC\r\n        A B C<\/pre>\n<p>The idea is to use recursion and create a buffer that one by one contains all output strings having spaces. We keep updating buffer in every recursive call. If the length of given string is \u2018n\u2019 our updated string can have maximum length of n + (n-1) i.e. 2n-1. So we create buffer size of 2n (one extra character for string termination).<br \/>\nWe leave 1st character as it is, starting from the 2nd character, we can either fill a space or a character. Thus one can write a recursive function like below.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20to%20print%20permutations%20of%20a%20given%20string%20with%20spaces.%0A%23include%20%3Ciostream%3E%0A%23include%20%3Ccstring%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F*%20Function%20recursively%20prints%20the%20strings%20having%20space%20pattern.%0A%20%20%20i%20and%20j%20are%20indices%20in%20\u2019str%5B%5D\u2019%20and%20\u2019buff%5B%5D\u2019%20respectively%20*%2F%0Avoid%20printPatternUtil(char%20str%5B%5D%2C%20char%20buff%5B%5D%2C%20int%20i%2C%20int%20j%2C%20int%20n)%0A%7B%0A%20%20%20%20if%20(i%3D%3Dn)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20buff%5Bj%5D%20%3D%20\u2019%5C0\u2019%3B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20buff%20%3C%3C%20endl%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Either%20put%20the%20character%0A%20%20%20%20buff%5Bj%5D%20%3D%20str%5Bi%5D%3B%0A%20%20%20%20printPatternUtil(str%2C%20buff%2C%20i%2B1%2C%20j%2B1%2C%20n)%3B%0A%20%0A%20%20%20%20%2F%2F%20Or%20put%20a%20space%20followed%20by%20next%20character%0A%20%20%20%20buff%5Bj%5D%20%3D%20\u2019%20\u2019%3B%0A%20%20%20%20buff%5Bj%2B1%5D%20%3D%20str%5Bi%5D%3B%0A%20%0A%20%20%20%20printPatternUtil(str%2C%20buff%2C%20i%2B1%2C%20j%2B2%2C%20n)%3B%0A%7D%0A%20%0A%2F%2F%20This%20function%20creates%20buf%5B%5D%20to%20store%20individual%20output%20string%20and%20uses%0A%2F%2F%20printPatternUtil()%20to%20print%20all%20permutations.%0Avoid%20printPattern(char%20*str)%0A%7B%0A%20%20%20%20int%20n%20%3D%20strlen(str)%3B%0A%20%0A%20%20%20%20%2F%2F%20Buffer%20to%20hold%20the%20string%20containing%20spaces%0A%20%20%20%20char%20buf%5B2*n%5D%3B%20%2F%2F%202n-1%20characters%20and%201%20string%20terminator%0A%20%0A%20%20%20%20%2F%2F%20Copy%20the%20first%20character%20as%20it%20is%2C%20since%20it%20will%20be%20always%0A%20%20%20%20%2F%2F%20at%20first%20position%0A%20%20%20%20buf%5B0%5D%20%3D%20str%5B0%5D%3B%0A%20%0A%20%20%20%20printPatternUtil(str%2C%20buf%2C%201%2C%201%2C%20n)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20char%20*str%20%3D%20%22ABCD%22%3B%0A%20%20%20%20printPattern(str)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming-Print all possible strings that can be made by placing spaces &#8211; Searching and Sorting -The idea is to use recursion and create a buffer.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,71670],"tags":[73466,73421,73469,73434,73438,73442,73452,73470,73463,73454,73464,73455,73445,73468,73456,73453,73450,73423,73436,73437,73431,73440,73429,73457,73461,73467,73444,73462,73432,73459,73422,73430,73433,73426,73425,73447,73448,73424,73446,73428,73465,73449,73441,73458,73439,73443,73427,73435,73460,73451],"class_list":["post-25467","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-searching-and-sorting","tag-c-printf","tag-c-programming-print-all-possible-strings-that-can-be-made-by-placing-spaces","tag-c-sprintf","tag-character-java","tag-format-java","tag-format-printf","tag-format-specifier-java","tag-format-specifiers-in-c","tag-how-to-take-input-from-user-in-java","tag-java-flag","tag-java-format","tag-java-format-print","tag-java-format-specifiers","tag-java-format-string","tag-java-print","tag-java-print-format","tag-java-print-line","tag-java-printf","tag-java-printf-double","tag-java-printf-example","tag-java-printf-format","tag-java-printf-string","tag-java-sprintf","tag-java-stdout","tag-java-string-format","tag-java-string-format-example","tag-java-string-printf","tag-java-system-out-format","tag-java-system-out-printf","tag-print-java","tag-printf","tag-printf-c","tag-printf-example","tag-printf-float","tag-printf-format","tag-printf-format-java","tag-printf-in-c","tag-printf-java","tag-printf-options","tag-printf-string","tag-string-format-java","tag-string-java","tag-string-printf","tag-string-programs-in-java","tag-system-out-format","tag-system-out-format-java","tag-system-out-printf","tag-system-out-printf-java","tag-system-printf","tag-using-printf"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25467"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25467\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}