cs50 vigenere - зацикливается неправильно - PullRequest
0 голосов
/ 20 ноября 2018

Извинения, если ответ на этот вопрос невероятно прост.Я просто не могу разобраться.

Я работал над проблемой CS50 Vigenere, и я думаю, что я почти на месте.Однако программа зацикливается таким образом, что я не ожидаю, и я не уверен, почему.После того, как он напечатал первый зашифрованный символ открытого текста, он возвращается назад, чтобы перейти к следующему символу в ключе, но пропускает ту часть, где ему нужно перейти к следующему символу простого текста.По крайней мере, я так думаю.

Вот мой код.Любая помощь будет принята с благодарностью.

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

int main(int argc, string argv [])
{
    int a;
    int ciphertext;
    int k;
    string plain;
    int cipher;
// check user has input a  valid number of arguments

    if (argc < 2 || argc > 2)
    {
        printf("please input a valid number of arguments\n");

        return 1;
    }
// check user has input a valid key and prompt for plaintext
    char * key = argv [1];


        for (a = 0; a < strlen(key); a++)
        if (!isalpha(key[a]))
        {
            printf("Please input a valid key. Key must be alphabetical");
            return 1;
        }



{
    if (a == strlen(key))
    {
        plain = get_string("Plaintext: ");
    }
{
    printf("ciphertext: ");
}
}


//read plaintext and keep track
{
    for (int i = 0, n = strlen(plain); i < n; i++)
    {
//read key and keep track
        if (isalpha(plain[i]))
        {
            for (int j = 0, p = strlen(key); j < p; j++)



//convert key to numerical

            {
                if (isupper(key[j]) > 'A')
                {
                    k = (key[j] - 65);
//calculate ciphertext and print (upper case)
                    {
                        printf("%c", (plain[i] + (k % p) %26) +65);
                    }
                }

            else if (islower(key[j]) > 'a')
            {
                k = (key[j] - 97);
                {
                    printf("%c", (plain[i] + (k % p) %26) +97);
                }
            }
            else printf("%c", plain[i]);


        }
    }
    }



    {
        printf("\n");
    }

}


}
...