I was writing this C program which takes in a command line argument , and then encrypts a text later entered by the user.
我正在编写这个C程序,它接受命令行参数,然后加密用户稍后输入的文本。
We enter a number through command line which is used in encryption. For eg , if we eter a number 1 from command line and then the user wants to encrypt say "aaaa" , the encrypted text will be "bbbb" , just that each has shifted one character. I made my output but there is no text printed back . What is my mistake?
我们通过命令行输入一个用于加密的数字。例如,如果我们从命令行输入数字1然后用户想要加密说“aaaa”,则加密文本将是“bbbb”,只是每个都移动了一个字符。我输出了输出,但没有打印出文字。我的错是什么?
Here is my code
这是我的代码
int main(int argc , string argv[])
{
if(argc != 2)
{
return -1;
}
int k = atoi(argv[1]);
printf("Enter the text to be encrypted : ");
string text = get_string();
int length = strlen(text);
for(int i=0 ; i<length ; i++)
{
int p = text[i];
int c = (p + k) % 26;
printf("%c",c);
}
}
1 个解决方案
#1
5
This code here:
这段代码在这里:
int c = (p + k) % 26;
printf("%c",c);
will most probably not print anything worthwhile, because the resulting values will be in the 0..25 range, that in ASCII are different control characters. What you want to do is to check if the character is ASCII lowercase or ASCII uppercase, and only then do the rotate, and ensure that the result is also in the same range:
很可能不打印任何有价值的东西,因为结果值将在0..25范围内,在ASCII中是不同的控制字符。你要做的是检查字符是ASCII小写还是ASCII大写,然后才进行旋转,并确保结果也在同一范围内:
unsigned int c = text[i];
if (c >= 'A' && c <= 'Z') {
c = ((c - 'A') + k) % 26 + 'A';
}
else if (c >= 'a' && c <= 'z') {
c = ((c - 'a') + k) % 26 + 'a';
}
printf("%c", c);
If the value is between 'A'
and 'Z'
, we subtract the code for 'A'
so that these map from 0
to 25
(note that we're assuming alphabetical consecutive ordering of character codes from A
to Z
here - this works on ASCII, but doesn't work on an IBM mainframe with EBCDIC, so consider yourself be warned!), then do the modulo math there, then add the code for 'A'
again to get into the original range.
如果值在'A'和'Z'之间,我们减去'A'的代码,使这些映射从0到25(请注意,我们假设字母代码从A到Z的字母连续排序 - 这是有效的在ASCII上,但不能在带有EBCDIC的IBM大型机上工作,所以请考虑自己被警告!),然后在那里进行模数运算,然后再次添加“A”代码以进入原始范围。
We do the same in else if
for lowercase letters.
如果是小写字母,我们在其他地方也这样做。
#1
5
This code here:
这段代码在这里:
int c = (p + k) % 26;
printf("%c",c);
will most probably not print anything worthwhile, because the resulting values will be in the 0..25 range, that in ASCII are different control characters. What you want to do is to check if the character is ASCII lowercase or ASCII uppercase, and only then do the rotate, and ensure that the result is also in the same range:
很可能不打印任何有价值的东西,因为结果值将在0..25范围内,在ASCII中是不同的控制字符。你要做的是检查字符是ASCII小写还是ASCII大写,然后才进行旋转,并确保结果也在同一范围内:
unsigned int c = text[i];
if (c >= 'A' && c <= 'Z') {
c = ((c - 'A') + k) % 26 + 'A';
}
else if (c >= 'a' && c <= 'z') {
c = ((c - 'a') + k) % 26 + 'a';
}
printf("%c", c);
If the value is between 'A'
and 'Z'
, we subtract the code for 'A'
so that these map from 0
to 25
(note that we're assuming alphabetical consecutive ordering of character codes from A
to Z
here - this works on ASCII, but doesn't work on an IBM mainframe with EBCDIC, so consider yourself be warned!), then do the modulo math there, then add the code for 'A'
again to get into the original range.
如果值在'A'和'Z'之间,我们减去'A'的代码,使这些映射从0到25(请注意,我们假设字母代码从A到Z的字母连续排序 - 这是有效的在ASCII上,但不能在带有EBCDIC的IBM大型机上工作,所以请考虑自己被警告!),然后在那里进行模数运算,然后再次添加“A”代码以进入原始范围。
We do the same in else if
for lowercase letters.
如果是小写字母,我们在其他地方也这样做。