我不确定我缺少用于pset2替换的代码。当我运行Check50时,它给出以下输出:
:) substitution.c exists
:) substitution.c compiles
:( encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
output not valid ASCII text
:( encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key
output not valid ASCII text
:( encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key
output not valid ASCII text
:( encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED as key
output not valid ASCII text
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZTEOGXHCIPJSQD as key
:) encrypts "This is CS50" as "Cbah ah KH50" using yukfrnlbavmwzteogxhcipjsqd as key
:) encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
:) encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key
:) handles lack of key
:) handles invalid key length
:) handles invalid characters in key
:) handles duplicate characters in key
:) handles multiple duplicate characters in key
正如人们可以看到的那样,我的代码在大多数情况下都可以正常工作,但是对于某些我无法弄清的代码,却给出了无效的ASCII文本输出。
这是我的代码的样子
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
else
{
if (strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters\n");
return 1;
}
else if (strlen(argv[1]) == 26)
{
for(int i = 0; i <= strlen(argv[1]); i++)
{
if (isdigit(argv[1][i]))
{
printf("Key must only contain alphabetic characters\n");
return 1;
}
}
for(int l = 0; l <= strlen(argv[1]); l++)
{
for(int j = 0; j < l; j++)
{
if(argv[1][j] == argv[1][l])
{
printf("Key must not contain repeated characters\n");
return 1;
}
}
}
}
}
string key = argv[1];
string uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string lowers = "abcdefghijklmnopqrstuvwxyz";
char upper_key[26], lower_key[26];
string text = get_string("Plain Text: ");
char ciphertext[strlen(text)];
for(int i = 0; i < 26; i++)
{
upper_key[i] = toupper(key[i]);
}
for(int i = 0; i < 26; i++)
{
lower_key[i] = tolower(key[i]);
}
for(int i = 0; i < strlen(text); i++)
{
if(isupper(text[i]))
{
for(int j = 0; j < 26; j++)
{
if (text[i] == uppers[j])
{
ciphertext[i] = upper_key[j];
}
}
}
else if(islower(text[i]))
{
for(int j = 0; j < 26; j++)
{
if(text[i] == lowers[j])
{
ciphertext[i] = lower_key[j];
}
}
}
else
{
ciphertext[i] = text[i];
}
}
printf("ciphertext: %s\n", ciphertext);
}
我对编码还很陌生,任何帮助将不胜感激...
答案 0 :(得分:2)
我也遇到了同样的问题
'\0'
在密文末尾。
编辑: 这是代码:
cypher_text[plain_txt_len] = '\0';
答案 1 :(得分:1)
由于没有终止Eugene指出的密文,因此您的代码是从密文外部读取的。以下是内存访问错误,这里是link,用于将来重现/调试任何段错误。
Plain Text:
Memory access error: reading from the outside of a memory space; abort execution.
# Reading 1 bytes from 0xffd04071 will read undefined values.
#
# The memory-space-to-be-read (start:0xffd0406c, size:5 bytes) is bound to 'ciphertext' at
# file:/substitution.c::49, 10
#
# 0xffd0406c 0xffd04070
# +------------------------------+
# | the memory-space-to-be-read |......
# +------------------------------+
# ^~~~~~~~~~
# the read starts at 0xffd04071 that is right after the memory-space end.
#
# Stack trace (most recent call first) of the read.
# [0] file:/musl-1.1.10/src/string/memchr.c::25, 9
# [1] file:/musl-1.1.10/src/stdio/vfprintf.c::602, 8
# [2] file:/musl-1.1.10/src/stdio/vfprintf.c::678, 8
# [3] file:/musl-1.1.10/src/stdio/printf.c::13, 8
# [4] file:/substitution.c::85, 5
# [5] [libc-start-main]