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

int shift(string argv[]);

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
            {
                // Converts alphabetical key to integers
                int k = shift(argv[1]);

                // Gets text from user
                string s = get_string("plaintext: ");
                printf("ciphertext: ");

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

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

int shift(string argv[])

{    
    // Iterates through each character of the key
    for (int j = 0, n = strlen(argv[1]); j < n; j++)
    {
        char k = argv[1][j];
        // Converts alphabetical characters to a numerical key
        if (islower[k])
        {
            j = j - 97;
        }
        else
        {
            j = j - 65;
        }
        return j;

    }
}
1

This may sound mean, but the shift function is a mess. First, there's the error of the parameter mismatch. Note that declaring a string is really declaring a char *. Then, the parameter is itself an array. By declaring it with int shift(string argv[])you've declared a pointer to a char pointer, or char **. Next, you've used argv as the parameter name. This is a really bad practice. You think you're using the argv[] array, but you're actually declaring a shadow array - an array that has the same name, but is totally unrelated. You should use a unique parameter name. Never use any of the "reserved" or default variable names like argc or argv in a declaration. Further, if you want to pass a string as a parameter, declare it as a string var, not an array. For example, you could use int shift(string mystring)

This would help you resolve that particular error, but there are bigger issues that will likely make this a moot point.

In reading your code, it's not clear what you were trying to do. Part of it looks like it is trying to cycle through the key string to 'shift' all of it's elements. Other parts look like it was trying to change only one char and return that value to the calling function. Then, it falls apart by trying to 'shift' j, which is the index value used to control the for loop. J has nothing to do with the key or any character in the key. BTW, the for loop will only execute once because of the return statements. The program will never cycle back to the top of the for loop. In short, the shift function as written can't make up its mind what it wants to do! A rewrite is in order.

Do you want to convert a single char or all the chars in a string? Will it only be used on the key or also on the plaintext?

And then, in the main program, how are you going to switch from one char to the next in the key?

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 .