打印所有可能的字符串,这些字符串可以通过在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
热门推荐
4 家长会简短祝福语
10 朋友升职红包祝福语简短
11 虎年幽默搞笑简短祝福语
12 虎年晋升祝福语大全简短
13 最新中午问候祝福语简短
14 晚上情侣祝福语大全简短
15 弟弟暖屋祝福语简短
16 个性留言简短英文祝福语
17 孩子成长祝福语老师简短
18 礼仪队元旦祝福语简短