- Related Questions & Answers
- Count all possible position that can be reached by Modified Knight in C++
- Print all possible strings of length k that can be formed from a set of n characters in C++
- All possible strings of any length that can be formed from a given string?
- Maximum elements that can be made equal with k updates in C++
- All combinations of strings that can be used to dial a number in C/C++?
- Print all possible expressions that evaluate to a target in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Maximum possible time that can be formed from four digits in C++
- Print all valid words that are possible using Characters of Array in C++
- How can Indian prisons be made secure?
- Count of numbers which can be made power of 2 by given operation in C++
- Can a constructor be made final in Java?
- Print all possible words from phone digits in C++
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Print all interleavings of given two strings in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given a string and we have to print all those string that can be made using this string by placing space in between the characters of the string.

Let’s take an example to understand the topic better −

Input: string = ‘XYZ’ Output: XYZ, XY Z, X YZ, X Y Z

To solve this problem, we will have to find all the possible ways in which we can put space in the string. For this we will use recursion. In this, we will place spaces on by one and generate a new string.

#include <iostream> #include <cstring> using namespace std; void printPattern(char str[], char buff[], int i, int j, int n){ if (i==n){ buff[j] = '\0'; cout << buff << endl; return; } buff[j] = str[i]; printPattern(str, buff, i+1, j+1, n); buff[j] = ' '; buff[j+1] = str[i]; printPattern(str, buff, i+1, j+2, n); } int main() { char *str = "XYZ"; int n = strlen(str); char buf[2*n]; buf[0] = str[0]; cout<<"The string generated using space are :\n"; printPattern(str, buf, 1, 1, n); return 0; }

The string generated using space are −

XYZ XY Z X YZ X Y Z

Advertisements