I am making a program in C which generates all the combinations of the given input.
For example,if input is ABCD,then it will give me an array of strings which will be
{"A","B","C","D","AB","BC","CD","ABC","BCD","ABCD"} without scrambling the word.
Here is my code:
char **word_generator(char *word)
{
char *combinations[500];
int index = 0;
int val = 0;
int index1 = 0;
int len = strlen(word);
for (int i = 0;i < pow(len,2);i++)
{
for (int j = 0;j < len - index;j++)
{
val = 0;
for (int k = j;k < j+index+1;k++)
{
combinations[index1][val] = word[k];
printf("%c ",word[k]);
val += 1;
}
printf("\n");
if (val == len)
{
return combinations;
}
index1 += 1;
}
index += 1;
}
}
I have tested the code and if gives perfect results if I don't bother about array and return value and just print the output.
But there are two problems in it.
- It gives the error
function returning address of a local variable. - Some problem is in the line
combinaitons[index1][val] = word[k];because I have tested the code and everything goes fine before that line.