- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- 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?
- Print all possible expressions that evaluate to a target in C++
- 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++?
- Recursively print all sentences that can be formed from list of word lists in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Print all valid words that are possible using Characters of Array in C++
- Maximum possible time that can be formed from four digits in C++
- C++ program to count maximum possible division can be made in a graph with given condition
- C++ program to find out the number of coordinate pairs that can be made
- C++ code to count maximum groups can be made
- Count of numbers which can be made power of 2 by given operation in C++
- Print all possible words from phone digits in C++

Advertisements