CS50凯撒打印随机字符

问题描述 投票:0回答:2

我对这个问题有点困惑,因为我可以解决这个问题并提交并获得了充分的信誉,但是当我在循环之前打印初始变量时,代码只显示单词。此代码有效:

int main(int argc, string argv[]) {
    // c = (p + k) % 26, where c is result text, p is input and k
    // is key

    //considers if arg count is two
    if (argc == 2) {
        int n = strlen(argv[1]);
        int check = 0;

        if (isdigit(argv[1][0])) {
            for (int i = 1; i < n; i++) {
                if (isdigit(argv[1][i]) || argv[1][i] == '0') {
                    check++;
                } else {
                    check--;
                }
            }
        }

        // verifies all characters are numeric
        if (check != n - 1) {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    } else {
        printf("Usage: ./caesar key\n");
        // returning 1 identifies an error and exits the program
        return 1;
    }

    int key = atoi(argv[1]);

    string plaintext = get_string("plaintext: ");

    printf("%i\n", key);
    printf("%s\n", plaintext);

    int m = strlen(plaintext);

    printf("%i\n", m);

    char ciphertext[m];
    int usekey = (key % 26);
    printf("%i\n", key);

    // NEED to figure out how to handle wrap around
    // need to understand ASCII
    for (int i = 0; i < m; i++) {
        int c = plaintext[i];

        //encrypts upper case letters
        if (c >= 65 && c <= 90) {
            //incorporates wrap around for uppercase
            if (c + usekey >= 90) {
                int val = 90 - c;
                int key2 = usekey - val;
                char cipher = 64 + key2;
                ciphertext[i] = cipher;
            }
            //considers if key works fine
            else {
                char cipher = c + usekey;
                ciphertext[i] = cipher;
            }
        }
        //encrypts lower case letters
        else if (c >= 97 && c <= 122) {
            //incorporates wrap around for lowercase
            if (c + usekey >= 122) {
                int val = 122 - c;
                int key2 = usekey - val;
                char cipher = 96 + key2;
                ciphertext[i] = cipher;
            } else {
                char cipher = c + usekey;
                ciphertext[i] = cipher;
            }
        } else {
        //encrypts punctuation
            ciphertext[i] = c;
        }
        printf("*\n");
    }

    printf("ciphertext: %s\n", ciphertext);

}

但是,此代码不起作用(对于使用1作为密钥将a加密为b,对于使用12作为密钥将world, say hello!加密为iadxp, emk tqxxa!)。正确答案后,它会随机打印不同的字符,我不知道为什么。

int main(int argc, string argv[]) {
    // c = (p + k) % 26, where c is result text, p is input and k
    // is key

    //considers if arg count is two
    if (argc == 2) {
        int n = strlen(argv[1]);
        int check = 0;

        if (isdigit(argv[1][0])) {
            for (int i = 1; i < n; i++) {
                if (isdigit(argv[1][i]) || argv[1][i] == '0') {
                    check++;
                } else {
                    check--;
                }
            }
        }

        // verifies all characters are numeric
        if (check != n - 1) {
            printf("Usage: ./caesar key\n");
            return 1;
        }
    } else {
        printf("Usage: ./caesar key\n");
        // returning 1 identifies an error and exits the program
        return 1;
    }

    int key = atoi(argv[1]);

    string plaintext = get_string("plaintext: ");

    int m = strlen(plaintext);

    char ciphertext[m];
    int usekey = (key % 26);

    // NEED to figure out how to handle wrap around
    // need to understand ASCII
    for (int i = 0; i < m; i++) {
        int c = plaintext[i];

        //encrypts upper case letters
        if (c >= 65 && c <= 90) {
            //incorporates wrap around for uppercase
            if (c + usekey >= 90) {
                int val = 90 - c;
                int key2 = usekey - val;
                char cipher = 64 + key2;
                ciphertext[i] = cipher;
            }
            //considers if key works fine
            else {
                char cipher = c + usekey;
                ciphertext[i] = cipher;
            }
        }
        //encrypts lower case letters
        else if (c >= 97 && c <= 122) {
            //incorporates wrap around for lowercase
            if (c + usekey >= 122) {
                int val = 122 - c;
                int key2 = usekey - val;
                char cipher = 96 + key2;
                ciphertext[i] = cipher;
            } else {
                char cipher = c + usekey;
                ciphertext[i] = cipher;
            }
        }
        //encrypts punctuation
        else {
            ciphertext[i] = c;
        }
    }
    printf("ciphertext: %s\n", ciphertext);
}
c cs50 caesar-cipher
2个回答
0
投票

我认为您的条件无法正常运行。您可以打印“ argv [1] [i]”并查看问题。这是我的代码可能会对您有所帮助。

bool isNumber(char number[])
{
    int i = 0;

    for (; number[i] != 0; i++)
    {
        if (!isdigit(number[i]))    //check if there is something that is not digit
        {
            return false;
        }
    }

    return true;
}

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

    if (argc == 2 && isNumber(argv[1]) == 1)
    {
        int k = atoi(argv[1]);
        string plainText, chipherText;
        plainText = get_string("plaintext: ");
        printf("ciphertext: ");
        for (int i = 0, n = strlen(plainText) ; i < n; i++)
        {
            // checking if it is lowercase 97 = a to 112 = z and if it + 13 characters along.
            if (plainText[i] >= 'a' && plainText[i] <= 'z')
            {
                printf("%c", (((plainText[i] - 'a') + k) % 26) + 'a'); // print out lowercase with key
            } // if it it between uppercase A and Z
            else if (plainText[i] >= 'A' && plainText[i] <= 'Z')
            {
                printf("%c", (((plainText[i] - 'A') + k) % 26) + 'A'); // print out uppercase with key
            }

            else

            {
                printf("%c", plainText[i]);
            }
        }

        printf("\n");

        return 0;
    }
    else if (argc != 2 || isNumber(argv[1]) == 0)
    {
        printf("Error\n");
        return 1;
    }
}

0
投票

您为m分配了cyphertext字节,这对于空终止符来说是不够的,您也没有设置空终止符,导致随机字符出现在加密输出之后。这实际上是未定义的行为,因此任何事情都可能发生,包括程序崩溃。

您不需要存储加密的文本,只需一次输出一个字节。另外,请勿使用6590等ASCII值,而应使用更具可读性的字符常量'A''Z'

这里是简化版:

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>

int main(int argc, string argv[]) {
    if (argc != 2) {
        printf("Usage: ./caesar key\n");
        // returning 1 identifies an error and exits the program
        return 1;
    }
    char *p;
    int key = strtol(argv[1], &p, 10);
    if (*p || p == argv[1]) {
        printf("caesar: invalid argument: %s\n", argv[1]);
        return 1;
    }
    string plaintext = get_string("plaintext: ");
    // assuming ASCII
    for (size_t i = 0; plaintext[i]; i++) {
        int c = plaintext[i];

        if (c >= 'A' && c <= 'Z') {
            c = 'A' + (c - 'A' + key) % 26;
        } else
        if (c >= 'a' && c <= 'z') {
            c = 'a' + (c - 'a' + key) % 26;
        }
        putchar(c);
    }
    putchar('\n');
    free(plaintext);
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.