为什么代码没有给出预期的密码?

问题描述 投票:0回答:1

我目前正在开发一个项目,该项目应该采用 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]);
         }

它有效,但没有考虑用户在文本中输入的大写和小写字母。请向我解释一下我的代码有什么问题吗?

c caesar-cipher
1个回答
0
投票

在检查您的代码和预期的输出时,我退后一步,得出了一种更通用的翻译/加密文本的方法。首先,每当我遇到涉及“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

进行一些基准测试后,该函数执行了所需的字符替换以生成密文。

© www.soinside.com 2019 - 2024. All rights reserved.