簡體   English   中英

為什么兩個不相等的字母在 C 中顯示為等價?

[英]Why do 2 letters not equal to each other show up as equivalent in C?

我正在制作一個對明文進行簡單加密並輸出加密密文的程序。 這是一個以YTNSHKVEFXRBAUQZCLWDMIPGJO為關鍵的示例。

$ ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
plaintext:  HELLO
ciphertext: EHBBQ

如果您想了解更多信息,可以閱讀此處的說明。

這是我的代碼:

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

string ciphertext_generator(string key,string plaintext);
bool alphabetic_char_checker(string word);
bool alphabetic_char_repeat_checker(string word);


string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Please enter a key\n ");
        return 1;
    }
    if (strlen(argv[1]) != 26)
    {
        printf("The key must contain 26 characters.\n ");
        return 1;
    }
    if (alphabetic_char_checker(argv[1]) == false)
    {
        printf("The key must only contain alphabetic characters.\n ");
        return 1;
    }
    else if (alphabetic_char_repeat_checker(argv[1]))
    {
        printf("The key must not contain repeated characters.");
        return 1;
    }


    string plaintext = get_string("plaintext: ");
    string ciphertext = ciphertext_generator(argv[1],plaintext);
    printf("ciphertext: %s",ciphertext);
    return 0;
}
string ciphertext_generator(string key,string plaintext)
{
    string ciphertext = plaintext;
    for (int i = 0, k = strlen(ciphertext); i < k; i++)
    {
        bool capital = isupper(ciphertext[i]);
        ciphertext[i] = toupper(ciphertext[i]);
        for (int i2 = 0, a = strlen(alphabet); i2 < a; i2++)
        {
            if (plaintext[i] == alphabet[i2])
            {
                if (capital == true)
                {
                    ciphertext[i] = key[i2];
                }
                else
                {
                    ciphertext[i] = tolower(key[i2]);
                }
            }
        }
    }
    return ciphertext;
}

bool alphabetic_char_checker(string word)
{
    for (int i = 0, w = strlen(word); i < w; i++)
    {
        word[i] = toupper(word[i]);
        if (word[i] < 'A' || word[i] > 'Z')
        {
            return false;
        }
    }
    return true;
}
bool alphabetic_char_repeat_checker(string word)
{
    for (int i = 0, w = strlen(word); i < w; i++)
    {
        word[i] = toupper(word[i]);
        for (int i2 = 0; i2 < w; i2++)
        {
            if (word[i] == word[i2])
            {
                return false;
            }
        }
    }
    return true;
}

我的問題在於ciphertext_generator function 接受密鑰和明文並輸出加密的密文。 這里發生的是它遍歷明文中的所有字母,然后對於每個字母,它遍歷整個字母表,檢查明文中的字母是否在字母表中,並計算它在字母表中的哪個索引,然后是然后用作密鑰的索引,然后在密文字符串中相應地替換。

這里的問題是,在明文的第一個字母上迭代字母表的每個字母,它在正確的索引 N 處正確找到並替換正確的字母,但它也替換了明文,而不僅僅是密文。

我不是想解決這個問題,只是想了解發生了什么,因為我已經調試過了。 接下來發生的事情是,第一個字母不斷迭代字母表的 rest,直到找到 N,然后用字母表中的正確字母替換它,但也用明文替換。

**

問題

當我只設置密文時,為什么明文會撿起字母?

回答問題

當我只設置密文時,為什么明文會撿起字母?

在您的 function ciphertext_generator的第一行它說string ciphertext = plaintext; 此行表明密文變量將等於明文變量,這意味着密文的 memory 指向與明文相同的 memory,因此對密文所做的任何更改都將應用於明文。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM