CS50 - pset2 - 替换 - "output not valid ASCII text"

CS50 - pset2 - Substitution - "output not valid ASCII text"

我的程序正在生成看似正确的输出,但当我 运行 check50 时,我仍然收到 :( 消息。我已经阅读了其他 awnsers类似的问题,但其中 none 似乎与我的问题实际上相似。

check50 输出:

:) substitution.c exists

:) substitution.c compiles

:) encrypts "A" as "Z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key

:) encrypts "a" as "z" using ZYXWVUTSRQPONMLKJIHGFEDCBA as key

:) encrypts "ABC" as "NJQ" using NJQSUYBRXMOPFTHZVAWCGILKED as key

:) encrypts "XyZ" as "KeD" using NJQSUYBRXMOPFTHZVAWCGILKED 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
    output not valid ASCII text

->:( encrypts "This is CS50" as "Cbah ah KH50" using YUKFRNLBAVMWZteogxhcipjsqd as key
    output not valid ASCII text

:) encrypts all alphabetic characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key

:) does not encrypt non-alphabetical characters using DWUSXNPQKEGCZFJBTLYROHIAVM as key

:) handles lack of key

:) handles too many arguments

:) handles invalid key length

:) handles invalid characters in key

:) handles duplicate characters in key

:) handles multiple duplicate characters in key

我将 -> 放在错误消息之前以便于查看

这很奇怪,因为就在这两个错误之前,有一个几乎相同的 input/output 已被检查为正确的

这是我的代码:

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

string subs(string plain, string key);

int key_check(string key);

int main(int argc, string argv[]) {

    // CHECK IF IT HAS AN INPUT
    if (argc < 2) {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    // CHECK IF IT HAS MORE THAN 1 INPUT
    if (argc > 2) {
        printf("Usage: ./substitution key\n");
        return 1;
    }

    // IF KEYCHECK FUNCTION DETECTS AN ERROR, RETURN 1
    if (key_check(argv[1]) == 1) {
        return 1;
    }
    // ELSE KEY = USER ARGV INPUT
    string key = argv[1];

    // GET USER PLAINTEXT INPUT
    string plain = get_string("plaintext:  ");

    string cipher = subs(plain, key);

    // PRINT RESULT
    printf("ciphertext: %s\n", cipher);

}

int key_check(string key) {
    // STRING LENGHT
    int leng = strlen(key);

    // CHECK IF KEY HAVE 26 CHARACTERS
    if (leng < 26) {
        printf("Key must contain 26 characters.\n");
        return 1;
    }


    for (int i = 0; i < leng; i++) {

        // CHECK IF KEY ONLY HAVE ALPHABET CHARACTERS
        if (isalpha(key[i]) == 0) {
            printf("Key must contain only alphabet characters\n");
            return 1;
        }

        // CHECK IF KEY HAVE REPEATED CHARACTER
        for (int i2 = 0; i2 < 26; i2++) {

            if (i != i2) {

                if (key[i] == key[i2]) {
                    printf("Key must have each character exactly one time\n");
                    return 1;
                }
            }
        }
    }

    return 0;
}

string subs(string plain, string key) {
    // GET PLAINTEXT LENGHT
    int leng = strlen(plain);

    // CREATES CIPHER STRING
    string cipher = plain;

    // CREATES AN ARRAY FOR UPPER KEY
    int UPPER[26];
    for (int i2 = 0; i2 < 26; i2++) {
        if (isupper(key[i2]) > 0 ) {
            UPPER[i2] = key[i2];
        }
        else {
            UPPER[i2] = key[i2] - 32;
        }
    }

    // CREATES AN ARRAY FOR LOWER KEY
    int LOWER[26];
    for (int i3 = 0; i3 < 26; i3++) {
        if (islower(key[i3] > 0)) {
            LOWER[i3] = key[i3];
        }
        else {
            LOWER[i3] = key[i3] + 32;
        }
    }

    for (int i = 0; i < leng; i++) {
        if (isupper(plain[i]) > 0) {
            cipher[i] = UPPER[plain[i] - 65];
        }
        else if (islower(plain[i]) > 0) {
            cipher[i] = LOWER[plain[i] - 97];
        }
        else {
            cipher[i] = plain[i];
        }
    }

    return cipher;

}

这一切让我认为这是一个 check50 问题,但由于我缺乏编码和解决问题的经验,它可以是任何问题。

提前致谢。

if (islower(key[i3] > 0)) { 行的括号位置错误。应该是:

    if( islower(key[i3]) > 0 ){

或(在 C 中更典型):

    if( islower(key[i3]) ){