possible permutations of a string

i am trying to print all possible combinations of a string but em getting garbage values.please help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <string.h>
void perm(int arr[],int k,int n)
{
    int temp,i;
    if(k==n)
    {   printf("\t");
        for(i=0;i<=n;i++)
            printf("%c",arr[i]);
    }
    else
        for(i=k;i<=n;i++)
    {
        temp=arr[i];
        arr[i]=arr[k];
        arr[k]=temp;
        perm(arr,k+1,n);
        temp=arr[i];
        arr[i]=arr[k];
        arr[k]=temp;
    }
}
int main()
{
    int k=0,n,i;
    char arr[100];
    printf("Enter String: ");
    gets(arr);
    n=strlen(arr)-1;
    perm(arr,k,n);
}
is there any reason why you are passing an int array to your permutation function?

Here is one way you could implement the permutation function. Or even look at to help understand how to implement a permutation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
http://en.cppreference.com/w/cpp/algorithm/next_permutation
template<class BidirIt>
bool next_permutation(BidirIt first, BidirIt last)
{
    if (first == last) return false;
    BidirIt i = last;
    if (first == --i) return false;
 
    while (1) {
        BidirIt i1, i2;
 
        i1 = i;
        if (*--i < *i1) {
            i2 = last;
            while (!(*i < *--i2))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first) {
            std::reverse(first, last);
            return false;
        }
    }
}


Also, may I ask why you are using an integer array for your permutation function?
oh tht where i made a mistake.tnx alot
Topic archived. No new replies allowed.