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

int shift(char c);

int main(int argc, string argv[])
{
    // Checks to make sure user enters correct number of command-line arguments
    if (argc != 2)
    {
        printf("Usage: ./vigenere keyword\n");
        return 1;
    }
    else
    {

        // Checks that user has only entered letters as a second command-line argument (the keyword)
        string key = argv[1];
        for (int i = 0, n = strlen(key); i < n; i++)
        {
            if (isalpha(key[i]) == 0)
            {
                printf("Invalid keyword\n");
                return 1;
            }
        }
        // Gets plaintext from user
        string plaintext = get_string("plaintext: ");
        printf("ciphertext: ");

        // Converts plain text to cipher text
        for (int l = 0, o = strlen(plaintext); l < o; l++)
        {
            int m = (l % strlen(key));
            if (islower(plaintext[l]))
            {
                char cipheredletter = (plaintext[l] + shift(key[m]) - 97) % 26 + 97;
                printf("%c", cipheredletter);
                m++;
            }
            else if (isupper(plaintext[l]))
            {
                char cipheredletter = (plaintext[l] + shift(key[m]) - 65) % 26 + 65; 
                printf("%c", cipheredletter);
                m++;

            }
            else
            {
                char cipheredletter = plaintext[l];
                printf("%c", cipheredletter);
            }

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

int shift(char c)
// Converts alphabetical character to a numerical key

{  
    if (islower(c))
    {
        c = c - 97;
    }
    else
    {
        c = c - 65;
    }
    return c;
}

2
  • There isn't enough description of the problem or sufficient code here to diagnose. Please edit the question and describe exactly what the problem is here. Also, add more code so that we can see what the different variables actually represent. Reminder: single letter variables are a bad practice, except as for loop indexes where the loops are short and simple. Var names should always be descriptive to eliminate confusion. If the for loop index is being used inside the loop for anything, it should have a descriptive name.
    – Cliff B
    Aug 27 '19 at 21:32
  • I've added all the code, with names for variables Aug 28 '19 at 21:46
0

The index var for the key and the index var for the plaintext must be completely independent of each other. The key index must only be incremented when a letter is processed and never when a non-alpha is processed, while the plaintext index var is incremented for every char processed.

This code has these two index variables, m and l, very tightly coupled! I'm surprised it works as well as it does!

BTW, I'll restate what I said before. Single letter vars bad. Descriptive vars good. Specifically, instead of l and m, how about keyIndex or plainIndex.

If this answers your question, please click on the check mark to accept. Let's keep up on forum mainteance.;-)

You must log in to answer this question.

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