Generate all strings under length N in C

后端 未结 2 633
清酒与你
清酒与你 2020-12-06 07:23

I tried coding this myself and horribly failed. This is basically what I want:

a
b
...
z
aa
ba
...
za
ab
bb
...
zz
aaa
baa
...
zzz

In the e

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 07:46

    Looks like you want it in C, here is a way to do it:

    #include 
    #include 
    
    int inc(char *c){
        if(c[0]==0) return 0;
        if(c[0]=='z'){
            c[0]='a';
            return inc(c+sizeof(char));
        }   
        c[0]++;
        return 1;
    }
    
    int main(void){
        int n = 3;
        int i,j;
        char *c = malloc((n+1)*sizeof(char));
        for(i=1;i<=n;i++){
            for(j=0;j

提交回复
热议问题