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
        for (int i = 0, n = strlen(argv[1]); i < n; i++)
        {    
            if (isalpha(argv[1][i]) == 0)
            {
                printf("Usage: ./vigenere keyword\n");
                return 1;
            }
            else
            {
                // Gets plaintext from user
                string s = get_string("plaintext: ");
                printf("ciphertext: ");

                // Obtains index value of each character in keyword
                int q, m;
                for (m = 0,  q = strlen(s); m < q; m++)
                {
                    m = (m % strlen(argv[1]));
                    m = shift(argv[1][m]);
                }
                printf("%i\n", m);

                // Converts plain text to cipher text
                for (int l = 0, o = strlen(s); l < o; l++)
                {

                        if (islower(s[l]))
                        {
                            char c = (s[l] + m - 97) % 26 + 97;
                            printf("%i\n", m);
                            printf("%c", c);
                        }
                        else if (isupper(s[l]))
                        {
                            char c = (s[l] + m - 65) % 26 + 65; 
                            printf("%c", c);
                        }
                        else
                        {
                            printf("%c", s[l]);

                        }
                }
                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;
}

0

You're correct that you're not indexing into the key, but it goes beyond that. It appears that the code is trying to use m to hold the key index, the value of the current key letter and the entire key, all at once. That's just not possible.

The code needs to handle all of these items separately. There should be a var specifically to be the key index - to select which letter of the key to use. You can use argv[1] as the key, but most people find it confusing, so I recommend doing a strcpy() and copy the key to a new string variable, maybe called key. Then the code can access each letter, like key[2] instead of the same letter at argv[1][2].

Work on separating the different functions to different vars and go from there. That should get you going.

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

You must log in to answer this question.

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