环绕数组导致c中的意外输出
Wrapping around array causing unexpected output in c
我希望数组中的值每隔 3 个字符重复一次,但我不明白如果它们引用相同的索引,为什么输出会不同。
输入:
Command line argument: baz
User input: barfoo
Output: caqmut
Expected output: caqgon
第一个 for 循环后 key_number[]
的值:
key_number[0] = 1
key_number[1] = 0
key_number[2] = 25
key_number[3] = 32767
key_number[4] = 32
key_number[5] = 57
使用的代码:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if(argc<=1)
{
printf("Usage: ./vigenere k");
exit(1);
}
// convert command line input from string to int varible keyword
string keyword = argv[1];
//prompt user input and initialized
printf("plaintext: ");
string plaintext = get_string();
printf("ciphertext: ");
//create lower_case_array
char lower_case[26] = "abcdefghijklmnopqrstuvwxyz";
//create upper_case_array
char upper_case[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//convert key from string to int[]
//int array_length =strlen(plaintext);
int key_number[6]; // [array_length]
int key_length = strlen(keyword);
//loop though key_number for the length of plaintext
for (int i = 0; i < (strlen(plaintext)); i++)
{
//if char is uppercase
if (keyword[i] >= 65 && keyword[i] < 91 )
{
key_number[i] = (keyword[i % key_length]) - 65;
}
// if char is lowercase
else if (keyword[i] >= 97 && keyword[i] < 123)
{
key_number[i] = (keyword[i % key_length]) - 97;
}
}
//print out letters accoring to ceasar cryptography
for (int i = 0; i < strlen(plaintext); i++)
{
//branch if letter is upper case
if (plaintext[i] >= 65 && plaintext[i] < 91 )
{
printf("%c", upper_case[((plaintext[i] - 65 + key_number[i]) % 26)]);
}
//branch if letter is lower case
else if (plaintext[i] >= 97 && plaintext[i] < 123)
{
printf("%c", lower_case[((plaintext[i] - 97 + key_number[i]) % 26)]);
}
//branch if any other character
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
}
当我阅读你的代码时让我感到困惑的是你有时使用 keyword[i % key_length]
有时 keyword[i]
.
因此,当您使用 i
时,您可以跳过 keyword
table:未定义的行为,因为 plaintext
可能比 keyword
长
您可以通过更干净地重写代码来避免这种情况:
for (int i = 0; i < (strlen(plaintext)); i++)
{
char kc = keyword[i % key_length];
//if char is uppercase
if (kc >= 'A' && kc <= 'Z' ) // aka isupper(kc)
{
key_number[i] = kc - 'A';
}
// if char is lowercase
else if (kc >= 'a' && kc <= 'z') // aka islower(kc)
{
key_number[i] = kc - 'a';
}
}
我希望数组中的值每隔 3 个字符重复一次,但我不明白如果它们引用相同的索引,为什么输出会不同。
输入:
Command line argument:
baz
User input:
barfoo
Output:
caqmut
Expected output:
caqgon
第一个 for 循环后 key_number[]
的值:
key_number[0] = 1
key_number[1] = 0
key_number[2] = 25
key_number[3] = 32767
key_number[4] = 32
key_number[5] = 57
使用的代码:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if(argc<=1)
{
printf("Usage: ./vigenere k");
exit(1);
}
// convert command line input from string to int varible keyword
string keyword = argv[1];
//prompt user input and initialized
printf("plaintext: ");
string plaintext = get_string();
printf("ciphertext: ");
//create lower_case_array
char lower_case[26] = "abcdefghijklmnopqrstuvwxyz";
//create upper_case_array
char upper_case[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//convert key from string to int[]
//int array_length =strlen(plaintext);
int key_number[6]; // [array_length]
int key_length = strlen(keyword);
//loop though key_number for the length of plaintext
for (int i = 0; i < (strlen(plaintext)); i++)
{
//if char is uppercase
if (keyword[i] >= 65 && keyword[i] < 91 )
{
key_number[i] = (keyword[i % key_length]) - 65;
}
// if char is lowercase
else if (keyword[i] >= 97 && keyword[i] < 123)
{
key_number[i] = (keyword[i % key_length]) - 97;
}
}
//print out letters accoring to ceasar cryptography
for (int i = 0; i < strlen(plaintext); i++)
{
//branch if letter is upper case
if (plaintext[i] >= 65 && plaintext[i] < 91 )
{
printf("%c", upper_case[((plaintext[i] - 65 + key_number[i]) % 26)]);
}
//branch if letter is lower case
else if (plaintext[i] >= 97 && plaintext[i] < 123)
{
printf("%c", lower_case[((plaintext[i] - 97 + key_number[i]) % 26)]);
}
//branch if any other character
else
{
printf("%c", plaintext[i]);
}
}
printf("\n");
}
当我阅读你的代码时让我感到困惑的是你有时使用 keyword[i % key_length]
有时 keyword[i]
.
因此,当您使用 i
时,您可以跳过 keyword
table:未定义的行为,因为 plaintext
可能比 keyword
长
您可以通过更干净地重写代码来避免这种情况:
for (int i = 0; i < (strlen(plaintext)); i++)
{
char kc = keyword[i % key_length];
//if char is uppercase
if (kc >= 'A' && kc <= 'Z' ) // aka isupper(kc)
{
key_number[i] = kc - 'A';
}
// if char is lowercase
else if (kc >= 'a' && kc <= 'z') // aka islower(kc)
{
key_number[i] = kc - 'a';
}
}