【问题标题】:C - Cycle through all possible lowercase stringsC - 循环遍历所有可能的小写字符串
【发布时间】:2019-08-10 09:19:19
【问题描述】:

我正在用 CS50 课程问题集 2 学习 C,使用 crypt 函数暴力猜出密码。目前正在编写一个打印所有可能的一定长度的字符串的函数,例如:

aa
ab
...
az
ba
...
zy
zz

为此,我编写了一个相当简单的递归函数:

#include <cs50.h>
#include <stdio.h>
#include <crypt.h>
#include <string.h>

void stringcycler(int n, int passLength, char *pass)
// Scrolls through all lowercase letter combinations for a string of length passLength
// Expects an integer value of the length of the strng as both n and passLength
// Also expects a char* array of length passLength with all chars set to 'a' (and a null character)
{
    if(n != 0)
    {
        for(pass[passLength - n] = 'a'; pass[passLength - n] < 'z'; pass[passLength - n]++)
        {            
            stringcycler(n-1, passLength, pass);
            printf("%s\n", pass);
            // return 0;
        }
    }
}


int main()
{    
    // Initialise char *c, and scroll through letters
    int passLength = 2; // The number of characters you want to brute force guess
    char pass[passLength + 1]; //  Add 1 for the null character
    int i;

    for(i = 0; i < passLength; i++) pass[i] = 'a'; // Set every char in pass to 'a'
    pass[passLength] = '\0'; // Set null character at the end of string

    stringcycler(passLength, passLength, pass);

    return 0;
}

它在大多数情况下都有效,但只适用于 yz。每当它看到 z 时,它基本上都会跳过,所以它会转到 yz,然后从不会 za 到 zz。如果我在 for 循环行中添加一个 =:

pass[passLength - n] < 'z';

即。

pass[passLength - n] <= 'z';

然后它在混合中打印“{”字符。有什么帮助吗?另一个问题是,我怎样才能改变它以适用于所有大小写组合,有没有一种简洁的方法?

【问题讨论】:

标签: c string recursion cs50 brute-force


【解决方案1】:

您在递归返回后打印,但您应该在递归到达字符串的结尾(或开头,在您的情况下)时打印。换句话说,打印应该是递归的替代分支:

void stringcycler(int n, int len, char *pass)
{
    if (n != 0) {
        for (pass[len - n] = 'a'; pass[len - n] <= 'z'; pass[len - n]++) {            
            stringcycler(n - 1, len, pass);
        }
    } else {
        printf("%s ", pass);
    }
}

if 部分在进一步向下递归时构造字符串。 else 部分对构造的字符串做了一些事情。 (当然,您必须在循环中包含'z'。您的原始代码仅在最后一个位置打印 z,因为它在递归返回后打印,这意味着 char 缓冲区处于不会(重新-)进入循环。)

【讨论】:

    【解决方案2】:

    下面是生成密码的通用回溯算法。这里的想法是想象填充给定字符数组a 的插槽。我们将为数组a 生成给定位置k 的可能候选者。我将候选人选为小写 ascii 字母 a-z 和大写 ASCII 字母 A-Z。如果要包含其他 ASCII 字符,只需相应地修改construct_candidates 函数。 一旦数组被填充,即 k 变为PASS_LEN,我们知道我们已经生成了密码,我们可以随意处理它,我刚刚在这里打印了密码。 PASS_LEN 宏的值可以调整以生成任意长度的密码。

    #include <stdio.h>
    #include <stdlib.h>
    #define PASS_LEN 2
    
    static char* construct_candidates (char a[], int k, int *count)
    {
        /* Lower case ASCII */
        int min1 = 97; 
        int max1 = 122; 
    
        /* Upper case ASCII */
        int min2 = 65; 
        int max2 = 90; 
    
        *count = (max1 - min1 + 1) + (max2 - min2 + 1); 
        char *cand = calloc(*count, sizeof(char));
        if (cand == NULL) {
            printf("malloc failed\n");
            return NULL;
        }   
        int idx = 0;
        for (int i = min1; i <= max1; i++) {
            cand[idx] = i;
            idx++;
        }   
        for (int i = min2; i <= max2; i++) {
            cand[idx] = i;
            idx++;
        }   
    
        return cand;
    }
    
    static void backtrack(char a[], int k)
    {
        int i;
        if (k == PASS_LEN) {
            for (i = 0; i < PASS_LEN; i++) {
                printf("%c", a[i]);
            }   
            printf("\n");
            return;
        }   
    
        int cand_count = 0;
        char *cand = construct_candidates(a, k, &cand_count);
        if (cand == NULL) {
            printf("Failed to get candidates\n");
            return;
        }   
        for (i = 0; i < cand_count; i++) {
            a[k] = cand[i];
            backtrack(a, k + 1); 
        }
        free(cand);
    }
    
    int main()
    {
        char a[PASS_LEN] = {'\0'};
        backtrack(a, 0); 
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2010-12-02
      • 2015-06-18
      • 2021-03-18
      • 1970-01-01
      • 2016-01-24
      • 2012-05-28
      • 2014-09-19
      相关资源
      最近更新 更多