CS50 Vigenere,代码几乎完成了,但是我不知道缺少了什么?

时间:2018-09-14 18:39:23

标签: c cs50

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

int main(int argc, string argv[])
{
    // two arguments
    if (argc != 2)
    {
        printf("Give two arguments\n");
        return 1;
    }
    printf("plaintext: ");
    string plaintext = get_string();
    printf("ciphertext: ");

    string key = argv[1];

    for (int i = 0, t = 0, n = strlen(plaintext); i < n; i++, t++)
    {
        // if it's no letter, then:

        if (!isalpha(plaintext[i]) && plaintext[i] != ' ')
        {
            printf("False");

            return 1;

        }

        int number = 0;

        if (isalpha(plaintext[i]))
        {
            number += 1;
        }

        if (strlen(key) > number)
        {
            number = 0;
        }


        if (isupper(plaintext[i]))
        {
            printf("%c", (((plaintext[i] - 65) + key[number]) % 26) + 65);
        }

        //if it is lowercase
        else if (islower(plaintext[i]))
        {
            printf("%c", (((plaintext[i] - 97) + key[number]) % 26) + 97);
        }

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

    }
    printf("\n");
}

因此我的代码中缺少一些内容。当我执行./vigenere baz并输入为明文:Hello, world!时,我得到密文:ByffiFalse。我应该得到iekmo, vprke!而且,当我键入./vigenere hello,然后键入bye作为明文时,我也得到了密文bye,而它应该是icp 。有人可以找出我的代码遗漏或存在的错误吗?

0 个答案:

没有答案