长度为 N 的字母表中所有字母的排列

发布于 2025-01-11 18:35:59 字数 1029 浏览 1 评论 0原文

我最近开始了一个用 C 语言处理的个人项目,涉及暴力密码破解和密码破解。加密。我一直在尝试开发一个输出长度为 N 的字母表的所有可能组合的函数。例如,如果 N = 4,则必须输出 aaaa - zzzz 的所有可能性。从逻辑上讲,我不明白应该如何递归地处理这个问题。

char* alphabet[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

void passwords(int size){
    char* password[size];
    char* result;
    //Determining static letter
    for(int i = 0; i < size; i++){
        for(int x = 0; x < size; x++){
            password[x] = "a";
        }
        int index = i+1; //password index to modify 
        while(index < size){
            for(int j = 0; j < 26; j++){
                password[i] = alphabet[j];
                printf("%s\n",password);
                
            }
            index++;
        }
    }
}
int main(int argc, char* argv[]){
    passwords(3);
    return 0;
}

目前,该程序仅修改字母表中的一个字符并产生以下输出:

aaa
baa
caa
daa
...//e-z aa
aaa
aba
aca
ada
...//a e-z a
aaa
aab
aac
aad
...//aa e_z

任何建议将非常感谢!

I recently began a personal project in C dealing with brute-force password cracking & encryption. I have been attempting to work on a function that outputs all possible combinations of the alphabet of length N. For example if N = 4, all possibilities from aaaa - zzzz would have to be outputted. Logically, I'm not understanding how I should approach this recursively.

char* alphabet[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

void passwords(int size){
    char* password[size];
    char* result;
    //Determining static letter
    for(int i = 0; i < size; i++){
        for(int x = 0; x < size; x++){
            password[x] = "a";
        }
        int index = i+1; //password index to modify 
        while(index < size){
            for(int j = 0; j < 26; j++){
                password[i] = alphabet[j];
                printf("%s\n",password);
                
            }
            index++;
        }
    }
}
int main(int argc, char* argv[]){
    passwords(3);
    return 0;
}

Currently this program only modifies one character of the alphabet and produces an output of:

aaa
baa
caa
daa
...//e-z aa
aaa
aba
aca
ada
...//a e-z a
aaa
aab
aac
aad
...//aa e_z

Any suggestions would be great thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2025-01-18 18:35:59

如果使用递归不是强制要求,请尝试:

#include <stdio.h>
#include <stdlib.h>
#define N 26                                    // number of alphabets

void passwords(int size) {
    int i;
    char *password;

    if (NULL == (password = malloc(size + 1))) {
        perror("malloc");
        exit(1);
    }

    for (i = 0; i < size; i++) {
        password[i] = 'a';                      // initialize the array
    }
    password[i] = '\0';                         // terminate the string

    while (1) {
        printf("%s\n", password);
        password[size - 1]++;                   // increment rightmost character
        for (i = size - 1; i >= 0; i--) {
            if (password[i] >= 'a' + N) {       // carry over
                if (i == 0) {                   // end of permutation
                    free(password);
                    return;
                } else {
                    password[i] = 'a';
                    password[i - 1]++;
                }
            }
        }
    }
}

int main()
{
    passwords(3);
    return 0;
}

If the usage of recursion is not the mandatory requirement, would you please try:

#include <stdio.h>
#include <stdlib.h>
#define N 26                                    // number of alphabets

void passwords(int size) {
    int i;
    char *password;

    if (NULL == (password = malloc(size + 1))) {
        perror("malloc");
        exit(1);
    }

    for (i = 0; i < size; i++) {
        password[i] = 'a';                      // initialize the array
    }
    password[i] = '\0';                         // terminate the string

    while (1) {
        printf("%s\n", password);
        password[size - 1]++;                   // increment rightmost character
        for (i = size - 1; i >= 0; i--) {
            if (password[i] >= 'a' + N) {       // carry over
                if (i == 0) {                   // end of permutation
                    free(password);
                    return;
                } else {
                    password[i] = 'a';
                    password[i - 1]++;
                }
            }
        }
    }
}

int main()
{
    passwords(3);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文