0

I got my Vigenere cipher to work except for the part where you are supposed to skip over a space or symbol while maintaining your position in the key. In other words, a space or symbol is being assigned a char from the key array.

It looks like this (second line is the key): This is CSfifty abcabcabcabcabc

When it should look like this: This is CSfifty abca bc abcabca

Here is my code so far:

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

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


// key position validation
if (argc != 2) 
{
    return -1;
}

// index into string key
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
    // key alpha validation
    if (!isalpha(key[i]))
    {
        return -1;
    }

    // zero key
    key[i] = tolower(key[i]);
    key[i] = key[i] - 97;
}

// plain text input
printf("Plain text:\n");
string plain = GetString();    

// encipher
for(int k = 0, p = strlen(plain); k < p; k++)
{        

    if(isalpha(plain[k]))
    {
        if(islower(plain[k]))
        {            
            printf("%c", (( (plain[k] - 97) + (key[k % keylen]) ) % 26 + 97));
        }   
        else if(isupper(plain[k]))
        {
            printf("%c", (( (plain[k] - 65) + (key[k % keylen]) ) % 26 + 65));
        }
    }
    else if(!isalpha(plain[k]))
    {
        printf("%c", plain [k]);


    }

}

printf("\n");

The code I just pasted does not even try to accommodate the space problem. I just pasted the version that works right now. I've been staring at my computer for three hours trying to figure out the problem but nothing seems to work. The closest I got to progress was when I created a separate for loop to create a new array that is the length of the plaintext and populated with the key (repeated to fill the length of the array). I was attempting to map the "plain-text-length key" before using it to print out the cipher text rather than enciphering the plaintext in real time in the cipher printf line as shown above. I tried subtracting from the counter and subtracting from the terminal int (to equal out the fact that I turned the clock back by 1.) But for some reason that ended up with the key repeating a single letter after encountering the space or symbol. (btw the reason why I made plainlenkey[j] = 'a' was so that the key didn't change the space or symbol in the cipher text since 'a' equaled 0.)

Here is what the for loop looked like:

char plainlenkey[strlen(plain)];

for(int j = 0, n = strlen(plain); j < n; j++)
{
    if(isalpha(plain[j]))
    {
        plainlenkey[j] = key[j % keylen];
    }

    else if(!isalpha(plain[j]))
    {
        plainlenkey[j] = 'a';
        n--;
        j--;
    }
}

And result looked something like:

This is CSfifty
ababaaaaaaaaaaa

I know this is a lot... so thanks in advance!

1 Answer 1

0

You are using the same index to move through both the plaintext and the key. The key and plaintext need two independent variables as indexes. Think about it for a minute. When you process a non-alpha in plaintext, you increment the index to get to the next letter. BUT, the only time you index to the next letter in the key is when you have encoded something. If you don't encode, you don't change the index of the key.

The two indexes are independent and totally unrelated. You can't use the same index for both.

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

1
  • Yea I had already figured out that I couldn't use the same index for the plaintext and key which is why I tried to make a separate for loop for the key, but after reading your response I realized I was waaaayyy over thinking it. I also got caught up in the idea that EVERYTHING needed a loop! hehehe... I just kept my original code and added a new int that started at 0 and and had a ++ for that int and used that as a counter for the key, while the index into the plaintext maintained it's original counter from the for loop! Thanks for the help again! Commented Oct 24, 2015 at 9:07

You must log in to answer this question.

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