根据密钥更改字母(加密)
Change letters according to a Key (Encryption)
我正在尝试使用给定密钥(变量 argv[1])通过密文对来自用户的输入(可变文本)进行加密。其中 A 替换为密钥的第一个字母,B 替换为密钥的第二个字母等
例如,如果我在终端输入:
~./test YTNSHKVEFXRBAUQZCLWDMIPGJO
然后用户输入以下文本 text: OVERFLOW
输出为:ciphertext: QIHLKBQP
我现在的问题是我的功能对以下用户输入不起作用:The quick brown fox jumps over the lazy dog
键 ./test DWUSXNPQKEGCZFJBTLYROHIAVM
我得到的:
ciphertext: Rqx tokug wljif nja eozby jhxl rqx c
我想得到的:
ciphertext: Rqx tokug wljif nja eozby jhxl rqx cdmv sjp
为什么我的代码在到达 c 后停止替换字母并停止添加 "... cdmw sjp"?
我当前的代码是:
#include <cs50.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int l = 97;
int j = 65;
int difference;
int differencee;
int k=0,b, c;
int main(int argc, string argv[])
{
string text = get_string("Plaintext: "); // Asking for the text-input
printf("ciphertext: ");
for(i = 0; i < strlen(text); i++, j++, l++) //loop for the given text length
{
difference = argv[1][i] - j;
differencee = argv[1][i] - l;
if(j==text[k] && text[k]<91 && text[k]>64) // Check for UPPERCASE LETTERS and change according to key
{
b = text[k] + difference;
k++;
printf("%c",toupper(b));
i = 0;
j = 64 +1;
l = 96 + 1;
}
if(l==text[k] && text[k]<123 && text[k]>96) //Check for LOWERCASE LETTERS and change according to key
{
c = text[k] + differencee;
k++;
printf("%c",tolower(c));
i = 0;
l = 96 + 1;
j = 64 + 1;
}
if(isalpha(text[k]) == 0 && text[k] != '[=10=]') //If not alphabet, then do nothing
{
printf("%c",text[k]);
k++;
i = 0;
}
}
printf("\n");
}
你可以简单地解决你的算法。不需要额外的计算和变量。
为您的示例检查这个并更改它以涵盖您的工作:
输入输出随心所欲
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
char text[64] = "The quick brown fox jumps over the lazy dog[=10=]"; // Asking for the text-input
char argv[32] = "DWUSXNPQKEGCZFJBTLYROHIAVM";
int len = strlen(argv);
int i=0;
char temp = 0;
int index = 0;
printf("strlen(text): %d strlen(argv) = %d\n ",strlen(text),len);
printf("ciphertext: ");
for(i = 0; i < strlen(text); i++) //loop for the given text length
{
if(isalpha(text[i]) == 0 && text[i] != '[=10=]') //If not alphabet, then do nothing
{
printf("%c",text[i]);
continue;
}
temp = text[i];
if(temp >= 'A' && temp <= 'Z') // Check for UPPERCASE LETTERS and change according to key
{
temp +=32;
}
if(temp >= 'a' && temp <= 'z') //Check for LOWERCASE LETTERS and change according to key
{
index = (temp - 'a')%len;
printf("%c",argv[index]);
}
}
printf("\n");
}
已编辑:
Online Demo
我正在尝试使用给定密钥(变量 argv[1])通过密文对来自用户的输入(可变文本)进行加密。其中 A 替换为密钥的第一个字母,B 替换为密钥的第二个字母等
例如,如果我在终端输入:
~./test YTNSHKVEFXRBAUQZCLWDMIPGJO
然后用户输入以下文本 text: OVERFLOW
输出为:ciphertext: QIHLKBQP
我现在的问题是我的功能对以下用户输入不起作用:The quick brown fox jumps over the lazy dog
键 ./test DWUSXNPQKEGCZFJBTLYROHIAVM
我得到的:
ciphertext: Rqx tokug wljif nja eozby jhxl rqx c
我想得到的:
ciphertext: Rqx tokug wljif nja eozby jhxl rqx cdmv sjp
为什么我的代码在到达 c 后停止替换字母并停止添加 "... cdmw sjp"?
我当前的代码是:
#include <cs50.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int l = 97;
int j = 65;
int difference;
int differencee;
int k=0,b, c;
int main(int argc, string argv[])
{
string text = get_string("Plaintext: "); // Asking for the text-input
printf("ciphertext: ");
for(i = 0; i < strlen(text); i++, j++, l++) //loop for the given text length
{
difference = argv[1][i] - j;
differencee = argv[1][i] - l;
if(j==text[k] && text[k]<91 && text[k]>64) // Check for UPPERCASE LETTERS and change according to key
{
b = text[k] + difference;
k++;
printf("%c",toupper(b));
i = 0;
j = 64 +1;
l = 96 + 1;
}
if(l==text[k] && text[k]<123 && text[k]>96) //Check for LOWERCASE LETTERS and change according to key
{
c = text[k] + differencee;
k++;
printf("%c",tolower(c));
i = 0;
l = 96 + 1;
j = 64 + 1;
}
if(isalpha(text[k]) == 0 && text[k] != '[=10=]') //If not alphabet, then do nothing
{
printf("%c",text[k]);
k++;
i = 0;
}
}
printf("\n");
}
你可以简单地解决你的算法。不需要额外的计算和变量。
为您的示例检查这个并更改它以涵盖您的工作: 输入输出随心所欲
#include <math.h>
#include <ctype.h>
#include <string.h>
int main()
{
char text[64] = "The quick brown fox jumps over the lazy dog[=10=]"; // Asking for the text-input
char argv[32] = "DWUSXNPQKEGCZFJBTLYROHIAVM";
int len = strlen(argv);
int i=0;
char temp = 0;
int index = 0;
printf("strlen(text): %d strlen(argv) = %d\n ",strlen(text),len);
printf("ciphertext: ");
for(i = 0; i < strlen(text); i++) //loop for the given text length
{
if(isalpha(text[i]) == 0 && text[i] != '[=10=]') //If not alphabet, then do nothing
{
printf("%c",text[i]);
continue;
}
temp = text[i];
if(temp >= 'A' && temp <= 'Z') // Check for UPPERCASE LETTERS and change according to key
{
temp +=32;
}
if(temp >= 'a' && temp <= 'z') //Check for LOWERCASE LETTERS and change according to key
{
index = (temp - 'a')%len;
printf("%c",argv[index]);
}
}
printf("\n");
}
已编辑: Online Demo