This is the program to print all possible combination of the string. It was orginally written in C++.
Now i'm trying to write it in C but stuck with this problem of strings and pointers + some warnings.
INPUT is taken from argument, which is "const **char" .
I tried to allocate the memory too but its of no use at all. :|

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

long fac(int n) {
    long f;
    for(f=1; n>1; n--) f*=n;
    return f;
}

int swap(char *tekst, int pos, int npos){
    char tmpchr=tekst[pos];
    tekst[pos]=tekst[npos];
    tekst[npos]=tmpchr;
    return 0;
}

int findcombo(char *tekst, int pos){
    char *tmpstr;
    int counter=1;
    int npos=pos;
    int fact=fac(strlen(tekst)-pos);
    while(counter<=fact) {
        if(pos<(strlen(tekst)-1)) pos++;
        if(fact>2){
            strcpy(tmpstr,tekst);
            counter+=findcombo(tekst,npos+1);
            strcpy(tekst,tmpstr);
            swap(tekst, npos, pos);
        }
        else {
            counter++;
            swap(tekst,npos,pos);
            printf("%s\n", tekst);
        }
    }
    return fact;
}


int main (int argc, char const* argv[])
{
    if(argc==0) {
        printf("Error: missing argument, string \n");
        return 1;
    }

    int len=strlen(argv[1]);
    printf("Combination: %ld\n", fac(len));
/*  char *test=calloc(sizeof(char), len+1);*/
/*  printf("%d",sizeof(test));*/
/*  strcpy(test, argv[1]);*/
/*  printf("<%s>\n", test);*/
    findcombo(argv[1],0);
/*  free(test);*/
    return 0;
}

This may miss the prblem but...

/* printf("%d",sizeof(test));*/

sizeof is a snare and delusion! It is evaluated by the compiler and in this case will tell you that test is pointer.
Sizeof does not look at what is pointed at.... use strlen...

Hope this is some help.

You are rite but, but not quite rite, I was just testing it ! its in comment!

You never defined tmpstr except as a pointer. There is no storage space associated. Therefore
strcpy(tmpstr,tekst); copies into never-never-land, and therefore blows up.

Thanx WaltP i guess i got little rusty! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.