在 C 中的 Caesar Cipher 中需要帮助
Need help in Caesar Cipher in C
我是编程新手,我正在尝试用 C 语言为凯撒密码编写程序。
输入包含一个等于字符串长度的整数 ilength,后跟字符串 str 和一个整数 encrypt。
我的输入是:
11
middle-Outz
2
输出:
okffng-Qwv@
要求的输出是:
okffng-Qwvb
下面是我写的代码。有人可以帮助我为什么我在输出中得到最后一个字符错误!
我一头雾水
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int ilength = 0, encrypt = 0, i = 0, j = 0;
char alph_base[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'X', 'Y', 'Z', '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'};
scanf("%d", &ilength);
char str[ilength + 1];
scanf("%s", str);
scanf("%d", &encrypt);
//printf("%c\n", str[5]);
char outputString[ilength + 1];
char temp[ilength + 1];
for (j = 0; j <= ilength; j++)
{
temp[j] = str[j];
i = 0;
if (str[j] == '[=13=]')
{
outputString[j] = '[=13=]';
}
while ((i >= 0) && (i < 26))
{
if (temp[j] == alph_base[i])
{
if (i == 25 && encrypt == 0)
{
outputString[j] = alph_base[25];
}
if ((i + encrypt) == 26)
{
outputString[j] = alph_base[(i + encrypt) % 26];
}
else
outputString[j] = alph_base[(i + encrypt) % 26];
}
if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)
outputString[j] = temp[j];
if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)
outputString[j] = temp[j];
i++;
}
while ((i > 25) && (i < 52))
{
if (temp[j] == alph_base[i])
{
if (i == 51 && encrypt == 0)
{
outputString[j] = alph_base[51];
}
if ((i + encrypt) == 51)
{
outputString[j] = alph_base[51];
}
if ((i + encrypt) > 51)
{
outputString[j] = alph_base[((i + encrypt) % 51) + 25];
}
else
outputString[j] = alph_base[(i + encrypt) % 51];
}
if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)
outputString[j] = temp[j];
if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)
outputString[j] = temp[j];
i++;
}
}
printf("%s\n", outputString);
return 0;
}
你的代码对于你想做的事情来说太复杂了。
您的问题似乎与从 'z'
循环回 'a'
有关。
像这样一个简单的函数就可以完成一个角色的工作:
#include <ctype.h>
char caesar_encrypt(char input, int key)
{
char output = input;
char base, offset;
// If not a letter, return the char unmodified
if (! isalpha(input))
{
return output;
}
base = isupper(input) ? 'A' : 'a'; // Check if upper/lower case
offset = input - base; // Take offset from 'a'
offset += key; // Add key to offset
offset %= 26; // Wrap offset to the 26 letters
output = base + offset;
return output;
}
这里有很多想法:
使用 <ctype.h>
中的函数(isalpha
、isupper
),这避免了代码中的许多比较。
将您的字符视为来自字母 A(大写或小写 A)的 'offset'。因此,您正在处理 [0;25]
范围内的数字,您可以使用简单的模数
进行换行
字符是'integers',所以你可以加减。要获得大写字母的第三个字母,你可以做char c = 'A' + 2;
,这比你的大数组更简单。
免责声明:此处编写的代码未经测试,可能包含拼写错误;)
NiBZ 完美回答。添加到它,如果您仍然想编写自己的检查而不是使用库函数,您可以执行以下操作:
int my_isalpha(char c){
return ((c >= 'a' && c <= 'z')|| (c >= 'A' && c <= 'Z'));
}
int my_isupper(char c){
return (c >= 'A' && c <= 'Z');
}
警告:以上实现适用于 ASCII,但不适用于 ISO 8859-1 或其相关标准。参考:
我是编程新手,我正在尝试用 C 语言为凯撒密码编写程序。
输入包含一个等于字符串长度的整数 ilength,后跟字符串 str 和一个整数 encrypt。
我的输入是:
11
middle-Outz
2
输出:
okffng-Qwv@
要求的输出是:
okffng-Qwvb
下面是我写的代码。有人可以帮助我为什么我在输出中得到最后一个字符错误!
我一头雾水
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int ilength = 0, encrypt = 0, i = 0, j = 0;
char alph_base[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'X', 'Y', 'Z', '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'};
scanf("%d", &ilength);
char str[ilength + 1];
scanf("%s", str);
scanf("%d", &encrypt);
//printf("%c\n", str[5]);
char outputString[ilength + 1];
char temp[ilength + 1];
for (j = 0; j <= ilength; j++)
{
temp[j] = str[j];
i = 0;
if (str[j] == '[=13=]')
{
outputString[j] = '[=13=]';
}
while ((i >= 0) && (i < 26))
{
if (temp[j] == alph_base[i])
{
if (i == 25 && encrypt == 0)
{
outputString[j] = alph_base[25];
}
if ((i + encrypt) == 26)
{
outputString[j] = alph_base[(i + encrypt) % 26];
}
else
outputString[j] = alph_base[(i + encrypt) % 26];
}
if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)
outputString[j] = temp[j];
if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)
outputString[j] = temp[j];
i++;
}
while ((i > 25) && (i < 52))
{
if (temp[j] == alph_base[i])
{
if (i == 51 && encrypt == 0)
{
outputString[j] = alph_base[51];
}
if ((i + encrypt) == 51)
{
outputString[j] = alph_base[51];
}
if ((i + encrypt) > 51)
{
outputString[j] = alph_base[((i + encrypt) % 51) + 25];
}
else
outputString[j] = alph_base[(i + encrypt) % 51];
}
if ((temp[j] < 65 || temp[j] > 90) && temp[j] < 97)
outputString[j] = temp[j];
if ((temp[j] < 97 || temp[j] > 122) && temp[j] > 90)
outputString[j] = temp[j];
i++;
}
}
printf("%s\n", outputString);
return 0;
}
你的代码对于你想做的事情来说太复杂了。
您的问题似乎与从 'z'
循环回 'a'
有关。
像这样一个简单的函数就可以完成一个角色的工作:
#include <ctype.h>
char caesar_encrypt(char input, int key)
{
char output = input;
char base, offset;
// If not a letter, return the char unmodified
if (! isalpha(input))
{
return output;
}
base = isupper(input) ? 'A' : 'a'; // Check if upper/lower case
offset = input - base; // Take offset from 'a'
offset += key; // Add key to offset
offset %= 26; // Wrap offset to the 26 letters
output = base + offset;
return output;
}
这里有很多想法:
使用
<ctype.h>
中的函数(isalpha
、isupper
),这避免了代码中的许多比较。将您的字符视为来自字母 A(大写或小写 A)的 'offset'。因此,您正在处理
[0;25]
范围内的数字,您可以使用简单的模数 进行换行
字符是'integers',所以你可以加减。要获得大写字母的第三个字母,你可以做
char c = 'A' + 2;
,这比你的大数组更简单。
免责声明:此处编写的代码未经测试,可能包含拼写错误;)
NiBZ 完美回答。添加到它,如果您仍然想编写自己的检查而不是使用库函数,您可以执行以下操作:
int my_isalpha(char c){
return ((c >= 'a' && c <= 'z')|| (c >= 'A' && c <= 'Z'));
}
int my_isupper(char c){
return (c >= 'A' && c <= 'Z');
}
警告:以上实现适用于 ASCII,但不适用于 ISO 8859-1 或其相关标准。参考: