【发布时间】:2018-03-13 20:44:17
【问题描述】:
我正在编写一个查找密码的程序。当我看到必须为所选密码长度的变量重复替换部分密码的“for”循环时,我遇到了一个问题。该程序的目标是生成和检查任何字符数组的密码,从“0”开始,经过“?(n次)”,其中“0”是第一个字符,“?”是最后一个字符。
有没有办法让 for 循环重复可变次数而不单独编码?
注意,基于广受好评的 cmets:
“重复一个 for 循环”可能更正确地表示为“嵌套几个 for 循环”。
int maxLength = 32; //Length of the password, changes via input
char output[maxLength] = "";
for (int currentLength = 1; currentLength < maxLength + 1; currentLength++) {
for (int characterNumber = 0; characterNumber < 96 /*characters found with switch/case, 95 total*/; characterNumber++) {
for (int position = currentLength /*position in string*/; position > 0; position--) {
//For loops of character and position currentLength times
char newCharacter = '\0'; //Avoiding not initialized error, could be and character
output[position - 1] = getChar(characterNumber, newCharacter);
}
}
}
输出示例如下: ...01、02、03...、10、11...、a0、a1...、????4afuh7yfzdn_)aj901...
【问题讨论】:
-
在你的 for 循环周围环绕一段时间
-
你的代码有什么问题?
-
@LioraHaydont 代码没有问题(据我所知,这只是对其进行粗略的重写),问题是它还没有完成,正如我在未完成代码的地方评论的那样去。否则我不知道该怎么做。
-
你可以通过递归获得n-deep for循环的效果
-
只要有一个流水号并将其转换为 base-96(或您拥有的任何字母大小)。不需要递归或嵌套循环。