打印所有可能的字符串,这些字符串可以通过在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
热门推荐
10 元旦的祝福语简短 公司
11 教师同事升职祝福语简短
12 生日果酒祝福语简短独特
13 中秋祝福语送朋友简短
14 相恋人回去祝福语简短
15 学生开心成语祝福语简短
16 婆婆高寿祝福语大全简短
17 学生祝福语简短古诗大全
18 金店立秋祝福语简短英文