为什么两个不相等的字母在 C 中显示为等价?
Why do 2 letters not equal to each other show up as equivalent in C?
我正在制作一个程序,对明文进行简单的加密并输出加密的密文。这是一个以 YTNSHKVEFXRBAUQZCLWDMIPGJO
为键的示例。
$ ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
plaintext: HELLO
ciphertext: EHBBQ
如果您想了解更多,可以阅读说明here。
这是我的代码:
#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
函数,该函数接受密钥和明文并输出加密的密文。这里发生的是它遍历明文中的每个字母然后对于每个字母,它遍历整个字母表检查明文中的字母是否在字母表中并计算它在字母表中的索引然后是然后用作密钥的索引,然后在密文字符串中相应地替换它。
这里的问题是,明文的第一个字母遍历字母表中的每个字母,它正确地找到并替换了正确索引处的正确字母,即 N,但它也替换了明文,而不仅仅是密文。
我不是想解决这个问题,只是想了解发生了什么,因为我已经调试过了。接下来发生的是第一个字母不断迭代字母表的其余部分,直到找到 N,然后用字母表中的正确字母替换它,但也用明文替换它。
**
问题
为什么我只设置了密文,而明文会提取字母?
回答问题
Why does plaintext pick up letters when I only set the ciphertext to do so?
在函数的第一行 ciphertext_generator
它说 string ciphertext = plaintext;
这行声明密文变量将等于明文变量,这意味着密文的内存指向与明文相同的内存因此对密文所做的任何更改都将应用于明文。
我正在制作一个程序,对明文进行简单的加密并输出加密的密文。这是一个以 YTNSHKVEFXRBAUQZCLWDMIPGJO
为键的示例。
$ ./substitution YTNSHKVEFXRBAUQZCLWDMIPGJO
plaintext: HELLO
ciphertext: EHBBQ
如果您想了解更多,可以阅读说明here。
这是我的代码:
#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
函数,该函数接受密钥和明文并输出加密的密文。这里发生的是它遍历明文中的每个字母然后对于每个字母,它遍历整个字母表检查明文中的字母是否在字母表中并计算它在字母表中的索引然后是然后用作密钥的索引,然后在密文字符串中相应地替换它。
这里的问题是,明文的第一个字母遍历字母表中的每个字母,它正确地找到并替换了正确索引处的正确字母,即 N,但它也替换了明文,而不仅仅是密文。
我不是想解决这个问题,只是想了解发生了什么,因为我已经调试过了。接下来发生的是第一个字母不断迭代字母表的其余部分,直到找到 N,然后用字母表中的正确字母替换它,但也用明文替换它。
**
问题
为什么我只设置了密文,而明文会提取字母?
回答问题
Why does plaintext pick up letters when I only set the ciphertext to do so?
在函数的第一行 ciphertext_generator
它说 string ciphertext = plaintext;
这行声明密文变量将等于明文变量,这意味着密文的内存指向与明文相同的内存因此对密文所做的任何更改都将应用于明文。