打印所有可能的字符串,这些字符串可以通过在C ++中放置空格来完成
在这个问题中,我们给了一个字符串,我们必须通过在字符串的字符之间放置空格来打印所有可以使用该字符串创建的字符串。
让我们举个例子来更好地理解这个话题-
Input: string = ‘XYZ’ Output: XYZ, XY Z, X YZ, X Y Z
为了解决这个问题,我们将必须找到所有可能的方式来在字符串中放置空格。为此,我们将使用递归。在这种情况下,我们将空格加一个并生成一个新字符串。
示例
#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;
}输出结果
使用空格生成的字符串是-
XYZ XY Z X YZ X Y Z
热门推荐
1 训练狗狗搜寻
2 如何训练狗狗装死
3 狗狗训练书
6 怎么训练狗狗拜年
8 狗狗训练游戏
10 狗狗嗅觉训练
11 怎么训练狗狗在笼子里不叫
12 怎么训练狗狗站立走路
13 怎么训练狗狗在马桶上
14 狗狗尿尿怎么训练
15 训练狗狗用品
16 泰迪狗狗的训练方法大小便
17 训练狗狗可以打吗
18 王曼丽的狗狗训练