在使用C++中的强力示例程序时遇到问题

8ehkhllq  于 2023-03-17  发布在  其他
关注(0)|答案(1)|浏览(106)

我现在正在学习C语言,我是一个新手,很抱歉有任何简单的错误,我想做一个蛮力程序,你在控制台输入一个单词,然后C程序将随机猜测,并将它们与你输入的单词进行比较。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. int main() {
  6. unsigned long guesses;
  7. char guess[100];
  8. char pass[100];
  9. char letter[100];
  10. const char letters[26] = "abcdefghijklmnopqrstuvwxyz";
  11. srand(time(NULL));
  12. printf("\ninput your password lowercase letters only: ");
  13. scanf("%99s", pass);
  14. while (strcmp(guess, pass) != 0) {
  15. //reset guess
  16. char guess[100] = {"\0"};
  17. //choose a random number
  18. //copy a random element from var letters
  19. //concentrate the letter to the guess and repeat till its as long as the password
  20. while (strlen(guess) < strlen(pass)) {
  21. char i = rand() % 26;
  22. strcpy(letter, &letters[i]);
  23. strcat(guess, letter);
  24. }
  25. printf("%s\n", guess);
  26. guesses++;
  27. }
  28. printf("your guess was %s and was found in %lu guesses", guess, guesses);
  29. return 0;
  30. }

控制台中的输出是我输入单词yes时的输出

  1. qrstuvwxyz
  2. efghijklmnopqrstuvwxyz
  3. efghijklmnopqrstuvwxyz
  4. stuvwxyz
  5. efghijklmnopqrstuvwxyz
  6. qrstuvwxyz
  7. bcdefghijklmnopqrstuvwxyz
  8. hijklmnopqrstuvwxyz
  9. qrstuvwxyz
  10. qrstuvwxyz

我真的不知道它为什么要这样做,如果有人能帮助它,将非常感激
我尝试了许多不同的方法来添加字母的猜测,但无论我尝试它添加更多的字母,然后我需要和字母增量为1,而不是一个新的随机字符每次

kpbpu008

kpbpu0081#

测试您的代码并对其进行调整以使其正常工作,下面是代码的重构版本。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #define MAX 100
  6. int main()
  7. {
  8. unsigned long guesses = 0;
  9. char guess[MAX];
  10. char pass[MAX];
  11. char letter[2];
  12. const char letters[26] = "abcdefghijklmnopqrstuvwxyz";
  13. srand(time(NULL));
  14. printf("\nInput your password lowercase letters only: ");
  15. scanf("%s", pass);
  16. while (1)
  17. {
  18. // Reset guess
  19. for (int j = 0; j < MAX; j++)
  20. {
  21. guess[j] = '\0';
  22. }
  23. // Choose a random number
  24. // Copy a random element from var letters
  25. // Concentrate the letter to the guess and repeat till its as long as the password
  26. while (strlen(guess) < strlen(pass))
  27. {
  28. letter[0] = letters[rand() % 26]; /* Set up character string with one random character */
  29. letter[1] = '\0';
  30. strcat(guess, letter);
  31. }
  32. printf("%s\n", guess);
  33. guesses++;
  34. if (strcmp(guess, pass) == 0)
  35. {
  36. break;
  37. }
  38. }
  39. printf("your guess was %s and was found in %lu guesses\n", guess, guesses);
  40. return 0;
  41. }

在这个经过调整的代码版本中需要注意的一些事项。

  • 当看到对固定值的重复引用时,通常习惯于使用“#define”声明来减少代码中的文字值。
  • 不是将猜测的测试作为“while”循环的真/假测试,而是建立一个简单的无限“while”循环,然后当做出正确的猜测时,从循环中中断,以消除猜测变量的模糊性和任何可能的范围问题。
  • “letter”字符串数组被简化为保存一个字符值沿着一个终止符,这简化了猜测字符串数组的连接。

下面是程序使用“bell”条目进行猜测时终端的最后几行输出。

  1. cjih
  2. nroe
  3. ppzk
  4. kyvi
  5. cofh
  6. bell
  7. your guess was bell and was found in 429653 guesses

正如上面的评论所指出的,输入值越长,这种暴力破解方法花费的时间就越长。当我用五个字符的文字测试这个程序时,程序运行了很长时间,直到我手动停止程序。因此,在这个程序中,可能有一个上限测试添加到尝试的次数上。
无论如何,您可以给予这个重构的代码,看看它是否符合您的项目的精神。

展开查看全部

相关问题