0

The output is almost completely correct aside from the ciphertext only corresponding to the first letter in the key.

 $ ./vigenere BaBa
Plaintext:Hello
IFMMP

Do I need to completely rethink how I arrnaged this?

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

int shift(char c);

int main(int argc, string argv[])
{
    if (argc != 2){
        printf("Usage: ./vigenere keyword\n");
    return 1;
    }

    string key = argv[1];
    int k = shift (key[0]);

    for (int i = 0; i < strlen(key); i++){

        if (!isalpha  (key[i])){
            printf("Usage: ./vigenere keyword\n");
            return 1;
        }
    }

    string p = get_string ("Plaintext:");

    for (int i = 0; i < strlen(p); i++ ){
        if islower (p[i])
            printf("%c", (p[i] + k) - 97);

        else if isupper (p[i])
                printf("%c", (p[i] + k) - 65);

         else
                printf("%c", (p[i]));

    }

    printf("\n");
    return 0;
}

int shift (char c)
{
    if (isalpha (c))
    {
        if (isupper(c))
        {
           int sk = (((c - 65) % 26) + 65);
            return sk;
        }
        else (islower(c));
        {
            int sk = (((c - 97) % 26) + 97);
            return sk;
        }

    }

    else
        return 1;
}
1

Your issue is that you are only using the first letter of the key.

int k = shift (key[0]);

You need to use each subsequent letter as you move along ciphering the plaintext. That is the challenge with vigenere versus caesar.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .