0

My vigenere code works fine when a single char or string is entered, however as soon as it detects a space in plaintext it gives wonky values. Please help!

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

void keySHIFTER(char a, char b, char c);

int main(int argc, string argv[])
{
    string key = argv[1];
    int n = strlen(key);

    if(argc == 2)
    {
        for(int i=0; i  < n; i++)
        {
            if(!isalpha(key[i]))
            {
                printf("Need a valid key \n");
                return 1;
            }
        }

        printf("plaintext:  \n");
        string plaintext = get_string();
        // printf("%s", plaintext);
        int l = strlen(plaintext);
        char c = 0;
        char y = 0;
        char c_key = 0;
        char y_key = 0;
        //char placeholder = 0;

        for(int i=0; i < l; i++ )

        {
            if ( isalpha( plaintext[i] ) )
            {

                if( isupper(plaintext[i]) && isupper(key[i]) )
                {
                    c_key = key[i] - 65;
                    y_key = (c_key) % 26;
                    key[i] = y_key;

                    c = plaintext[i] - 65;
                    y = (c  + key[i]) % 26;
                    y = y + 65 ;
                    plaintext[i] = y;
                }    
                else if( islower(plaintext[i]) && islower(key[i]) )
                {
                    c_key = key[i] - 97;
                    y_key = (c_key) % 26;
                    //y_key = y_key + 97;
                    key[i] = y_key;

                    c = plaintext[i] - 97;
                    y = (c  + key[i]) % 26;
                    y = y + 97 ;
                    plaintext[i] = y;
                }
                else if(islower( plaintext[i]) && isupper(key[i]) )
                {
                    c_key = key[i] - 65;
                    y_key = (c_key) % 26;
                   // y_key = y_key + 65;
                    key[i] = y_key;

                    c = plaintext[i] - 97;
                    y = (c  + key[i]) % 26;
                    y = y + 97 ;
                    plaintext[i] = y;  
                }
                else if(isupper( plaintext[i]) && islower(key[i]) )
                {
                    c_key = key[i] - 97;
                    y_key = (c_key) % 26;
                    //y_key = y_key + 97;
                    key[i] = y_key;

                    c = plaintext[i] - 65;
                    y = (c  + key[i]) % 26;
                    y = y + 65 ;
                    plaintext[i] = y;
                }


            }
        }

        printf("ciphertext:  %s \n",plaintext);
        printf("\n");
        return 0;
    }
    else
    {
        printf("Need exactly two command line arguments.\n");
        return 1;
    }
}

void keySHIFTER(char a, char b, char c)
{
    a = c - 65;
    b = a % 26;
    //y_key = y_key + 65;
    c = b;

}
0

1. There are two different indices. The position within the plaintext string, and the position within the key.

These indices not always move the same. While the position in the plaintext string moves once per loop iteration, the position within the key should move only when encrypting a letter. The position within the key needs to somehow wrap around, after the last letter of the key you'd start with the first again.

2. You should read argv[1] only after you have checked this element exists. Especially, you should not call strlen(argv[1]) before making sure it exists, as strlen will loop to the first zero value, which might be at some place in memory where you are not allowed to read.

3. Also, please do not change the key array/string. Changing the content of the command-line arguments is undefined in behaviour, and you would have to make sure you change each letter exactly once, and you no longer could use char or string functions (like islower or isupper) on that result, as they could contain a 0 as a regular value, while in C strings it has the special meaning of the string terminator.

You must log in to answer this question.

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