首页 > 解决方案 > CS50 Caesar 打印随机字符

问题描述

我对这个问题有点困惑,因为我让它工作并提交并获得了完整的信用,但是当我在循环之前打印初始变量时,代码只有文字。此代码有效:

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使用12 作为密钥进行加密)。它在正确答案后随机打印不同的字符,我不知道为什么。bworld, 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);
}

标签: ccs50caesar-cipher

解决方案


我认为您的 if 条件无法正常工作。您可以打印 '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;
    }
}

推荐阅读