0

Using the C language I have to write a program that computes all possible combinations of a given string.

Example Input:

ABC

Example Output:

A,B,C,AB,AC,BC,ABC.

I wrote this code and it works for my example input, but currently it works only for Strings which are exactly 3 characters long. I want to modify my program so that it can accept a string of N characters lenght and still give the correct output.

This is my code so far:

int position1 = 0;
int position2 = 0;
char conjuntoQ[100]="\0";
size_t len;
while (m1.q[position1] != NULL) {
   position2 = position1;
   position2++;
   while (m1.q[position2] != NULL) {
     len = strlen(conjuntoQ);
     conjuntoQ[len] = m1.q[position];
     len = strlen(conjuntoQ);
     conjuntoQ[len] = m1.q[position2];
     len = strlen(conjuntoQ);
     conjuntoQ[len] = ',';
     position2++;
    }
position++;
}
Magisch
  • 7,312
  • 9
  • 36
  • 52
Fernando
  • 71
  • 1
  • 1
  • 8
  • What concrete problem are you facing with your task? Please take the time to show what you have tried and formulate a clear problem statement and any errors you encounter. – Magisch Jun 20 '16 at 06:04
  • my problem is that my function only combines a string assuming that its 3 characters long for example: ABC, if you look at my code it has 2 while, if i want it to support a string with 4 characters like: ABCD i would have to add another while to my code. what can i do so that my code could accept a string with N characters without adding more whiles. – Fernando Jun 20 '16 at 06:08
  • You tried a recursive approach yet? – Magisch Jun 20 '16 at 06:14
  • Right, I edited your question a bit and rewrote some of the wording to get your problem across better. – Magisch Jun 20 '16 at 06:17
  • @Fernando - did you ask about *all permutations* ? Your example output is not an example of all permutations. In other words - is this really a dup of the other question? – Support Ukraine Jun 20 '16 at 06:34
  • on more investigation i found that the correct therm would be power of a set, and i already found a way of doing this. – Fernando Jun 23 '16 at 21:48

0 Answers0