C语言 为什么代码没有给出预期的密码词

busg9geu  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(109)

我目前正在做一个项目,这个项目需要一个26个字符的密钥,这个密钥基本上是一个新的字母表,这样人们就可以加密一个单词。
例如,如果用户输入键VCHPRZGJNTLSKFBDQWAXEUYMOI和单词"Hello",控制台应该回答“jrssb“。目前,如果我输入单词“hello“,控制台打印出“MOAAB“,我不明白为什么它会这样做。```

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


string cipher_word( string key , string text){

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'};

int i = 0 ;
int j = 0 ;
string cipher = text;
while(text[i] != '\0'){//iterates through every char in the string

    while(j < 26){

   char tmpKey;
   char tmpAlphabet;
        tmpKey = key[j];
        tmpAlphabet = alphabet[j];

            if(islower(text[i])){
                tmpAlphabet = tolower(tmpAlphabet);
            }//makes it so the loop can iterate through lowercase letters

       if(text[i] == tmpAlphabet){
          if(islower(cipher[i])){
                cipher[i] = tolower(tmpKey);
            }//is supposed to make the word lowercas if the user inputs a word in lowercase
            cipher[i] = tmpKey;
            }//ciphers the word
            j++;
    }
    j = 0;
    i++;
    }

return cipher;
}

int main(int argc, string argv[])
{

string key = get_string("What is the key? ");
string text = get_string("what text do you want to cipher? ");
string cipher = cipher_word(key,text);

printf("%s\n",cipher);
}

我尝试添加以下内容:

if(isupper(text[i])){
             text[i] = tolower(text[i]);
         }

它工作,但它没有考虑到用户在文本中输入的字母和字母。请解释一下我的代码有什么问题?

vfh0ocws

vfh0ocws1#

在检查您的代码以及预期的输出时,我后退了一步,推导出一种更通用的翻译/加密文本的方法。首先,每当我遇到涉及“CS50”函数的在线教程中出现的问题时,我通常会选择用更通用的方法和函数来替换这个库集中的“CS50特定”功能,特别是当涉及到字符串操作时。
考虑到这一点,下面是一个重构版本的翻译函数和数据输入在终端。
给予一个尝试。希望这能澄清字符匹配和翻译。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void cipher_word(char * key, char * text, char * cipher)
{
    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'};

    int i = 0 ;
    int up;

    while(text[i] != '\0')                          // Iterates through every char in the string
    {
        up = isupper(text[i]);                      // Establish whether or not this character is upper case - use for later

        for (int j = 0; j < 26; j++)
        {
            if (toupper(text[i]) == alphabet[j])    // Find the current character in the alphabet for this text character
            {
                cipher[i] = toupper(key[j]);        // Just in case the key was entered in lower case or mixed case
                break;
            }
        }

        if (up == 0)
            cipher[i] = tolower(cipher[i]);         // Make the translated character lower case if the original text character was lower case

        i++;
    }

    return;
}
int main(int argc, char* argv[])
{
    char key[50];                                   // Utilizing standard character array definitions for strings
    char text[20];
    char cipher[20];

    printf("What is the key? ");                    // Utilizing standard prompting for input data
    scanf("%s", key);

    printf("What text do you want to cipher? ");
    scanf("%s", text);

    cipher_word(key, text, cipher);

    printf("%s\n",cipher);

    return 0;
}

关键部分是:

  • 字符数组(字符串)的定义
  • 支票上的字母

有了这些简化,下面是一个测试在终端的几个字。

craig@Vera:~/C_Programs/Console/CipherText/bin/Release$ ./CipherText 
What is the key? vchprzgjntlskfbdqwaxeuymoi
What text do you want to cipher? Hello
Jrssb
craig@Vera:~/C_Programs/Console/CipherText/bin/Release$ ./CipherText 
What is the key? vchprzgjntlskfbdqwaxeuymoi
What text do you want to cipher? Chapter
Hjvdxrw

在进行一些台架测试时,该函数执行了所需的字符替换以生成密码文本。

相关问题