CS50 -PSET2-替换 - “输出无效ASCII文本”

发布于 2025-02-04 05:43:22 字数 3865 浏览 2 评论 0原文

我的程序正在产生看起来像正确的输出,但我仍然会收到 :(运行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

#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;

}

​认为这是一个检查问题,但是由于我缺乏编码和解决问题的经验,因此可以

提前。

My program is producing what is seems like the correct output but i still get the :( message when i run check50. I have already read other awnsers to similar questions but none of them seems actually similar to my problem.

check50 output:

:) 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

I put the -> before the error messages for easier visualization

It is weird because right before the two errors there is an almost identical input/output that has been checked as correct

Here is my code:

#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;

}

It all leads me to think that it is a check50 problem, but with my lack of experience with coding and problem solving it can be anything.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2025-02-11 05:43:22

线if(islower(key [i3]&gt; 0)){在错误的位置括号。应该是:(

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

在C中更典型):

    if( islower(key[i3]) ){

The line if (islower(key[i3] > 0)) { has parentheses in the wrong place. It should be:

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

or (more typical in C):

    if( islower(key[i3]) ){
海的爱人是光 2025-02-11 05:43:22

要获得上下工作。

小写需要指向i2。

for (int i2 = 0; i2 < 26; i2++) {
    if (islower(key[i2] > 0)) {
        LOWER[i2] = key[i2];
    }
    else {
        LOWER[i2] = key[i2] + 32;

To get upper and lower working.

Lowercase needs to point to i2.

for (int i2 = 0; i2 < 26; i2++) {
    if (islower(key[i2] > 0)) {
        LOWER[i2] = key[i2];
    }
    else {
        LOWER[i2] = key[i2] + 32;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文