GeekInterview.com
Series: Subject: Topic:
Question: 194 of 587

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<
Asked by: Debananda | Member Since Aug-2007 | Asked on: Aug 30th, 2007

View all questions by Debananda

Showing Answers 1 - 1 of 1 Answers
jv500

Answered On : Sep 13th, 2009

View all answers by jv500

#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 ;
}


  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.