Write sample code or algorithim to get all possible combinations of data that will be entered from keyboard


like, If i enter 1 2 3 then it should show 1 2 3
1 3 2
2 1 3
All 6 possible combnations<

Questions by Debananda

Showing Answers 1 - 1 of 1 Answers

jv500

  • Sep 13th, 2009
 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Program to print all combinations of an input */

char *globalPtr;

void swap (char *a, char *b)
{
char t = *a;

    *a = *b;
    *b = t;
}

void displayComb (char *s, size_t len)
{
int i;

    if (s == NULL) {
        return ;
    }

    if (len == 1) {
        printf ("%sn", globalPtr);
        return ;
    }

    displayComb (s+1, len-1);
    for (i = 1; i < len; i++) {
        swap (s, s+i);
        displayComb (s+1, len-1);
        swap (s+i, s);
    }
    return ;
}


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions