Vigenere cypher program error for cs50

时间:2015-10-30 22:16:47

标签: c vigenere cs50

I'm writing a Vigenere cypher program in C for CS50 and have it working almost perfectly. The error is that sometimes when the encryption wraps around the output is a ? symbol in a white circle.

Below is my code;

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

int main(int argc, string argv[])
{
char a;
char letter;
char cryptletter;
char passletter;
string message;
int messcount = 0;

if(argc != 2)
{
    printf("Command line must contain one passphrase\n");
    return 1;
}

for(int i=0; i < strlen(argv[1]); i++)
{
    a = argv[1][i];

    if(isalpha(a) == 0)
    {
        printf("Encryption keyphrase must only contain alphabetic characters\n");
        return 1;
    }
}


message = GetString();


for (int j = 0; j < strlen(message); j++)
{
    letter = message[j];

    if(passletter < 91 && passletter > 64)
    {
        passletter = passletter + 32;
    }

    if(isupper(letter) != 0) //if uppercase 65-90
    {
        passletter = argv[1][messcount];
        cryptletter = passletter + letter - 97;

        if(cryptletter > 90)
        {
            cryptletter = cryptletter - 25;
        }

        printf("%c", cryptletter);
        messcount++;
    }

    if(islower(letter) != 0) //if lowercase 97-122
    {
        passletter = argv[1][messcount];
        cryptletter = passletter + letter - 97;

        if(cryptletter > 122)
        {
            cryptletter = cryptletter - 25;
        }

        printf("%c", cryptletter);
        messcount++;
    }


    if(messcount > strlen(argv[1])-1)
    {
        messcount = 0;
    }

    if (isalpha(letter) == 0)
    {
        printf("%c", letter);
    }
}

printf("\n");
}

Any help would be greatly appreciated, having a similar problem with the caesar cypher. Thanks

1 个答案:

答案 0 :(得分:1)

Here:

if(passletter < 91 && passletter > 64)

passletter is uninitialized