我找到了一些我想做的事的例子,但是,当然没有一个能完全按照我的意愿工作。
我正在尝试修改以下程序以显示字符串的所有可能组合。因此,例如:
*str = "one two"; // would be:
one two
two one
*str = "one two three"; // would be:
one two three
one three two
two one three
two three one
three one two
three two one
等等..
这是我正在使用的东西,它也产生重复的东西,这是我不想要的。
#include <stdio.h>
#include <string.h>
/* Function to swap values at two pointers */
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int l, int r)
{
int i;
if (l == r)
printf("%s\n", a);
else
{
for (i = l; i <= r; i++)
{
swap((a+l), (a+i));
permute(a, l+1, r);
swap((a+l), (a+i)); //backtrack
}
}
}
/* Driver program to test above functions */
int main()
{
char str[] = "one two three";
int n = strlen(str);
permute(str, 0, n-1);
return 0;
}
最佳答案
代替
char str[] = "one two three";
尝试从
char *strs[3];
strs[0] = "one";
strs[1] = "two";
strs[2] = "three";
然后修改您现有的算法以使用该算法。
关于c - 单词排列生成器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48776323/