我正在通过 CS50 类(class)问题集 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';
然后它会打印混合中的“{”字符。有什么帮助吗?另一个问题是,如何更改它以适用于所有大小写组合,是否有一种巧妙的方法?
最佳答案
从递归返回后打印,但应该在递归到达字符串的末尾(或开始,在您的情况下)时打印。换句话说,打印应该是递归的替代分支:
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 缓冲区位于不会(重新)进入循环的条件。)
关于C - 循环遍历所有可能的小写字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55247912/