0

Help me please with a problem on coding vigenere.c.

The problem is with figuring out the correct code for using the key-formula on the plaintext.

I think that i've found out the correct formula for the key with chars loop. The concept is that when the char counter reaches the length of the string argv[1] it starts from [0] char again. I've tested this with printf and it worked correctly. The formula i use is:

j = (strlen(k) - i) % (strlen(k))

But, unfortunately, i cannot find out how to apply this in a correct way for crypting the plaintext message. When i crypt, i get it crypted but definitely not the way it should be.

My full code is below. Please don't make the correct code for me. I just need an advise. Thank you in advance.

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

int main(int argc, string argv[])
{ 

    if (argc != 2)
    {
        printf("The key is not correct\n");
        return 1;
    }

    for (int i = 0; i < strlen(argv[1]); i++)
    {
        if (!isalpha(argv[1][i]))
            {
                printf("The key is not correct\n");
                return 1; 
            }
    }

    string p = GetString();
    int n = strlen(p);
    string k = argv[1]; 
    int j = 0;    
    for (int i = 0; i < n; i++)
    {   

        // Declare conditions for usage of the key  
        if (isalpha(p[i]))
        {

            if (i < strlen(k))
            {
                j = i;
            }

            else if (i >= strlen(k))
            {             

                j = (strlen(k) - i) % (strlen(k));
            }             
                if (isupper(k[j]))
                {
                    k[j] -=  'A';
                }

                else if (islower(k[j]))
                {
                    k[j] -= 'a';
                }       
            } 

        // Crypt chars for lowercase letters
        if (islower(p[i]))
        {      
            p[i] = (p[i] - 'a' + k[j]) % 26 +'a';       
        }

        // Crypt chars for upper case letters
        else if (isupper(p[i]))
        {    
            p[i] = (p[i] - 'A' + k[j]) % 26 + 'A';         
        }    
    } 

    // Print the crypted message
    printf("%s\n", p);            

    return 0;
}
1

I suspect that you are encoding characters, but incorrectly. That's because you are selecting the wrong char from the key to encode. Try working through the code with paper and pencil and sample data to convince yourself.

Your formula for which character to use in the key string is incorrect. The index for your key, j, is completely independent from i, the index for the plain string. There is no correlation between the two, especially after you start processing non-alphas. Simply put, you increment the index for the plainstring for every char processed, BUT you only increment the index for the key when you actually consume a char in the key (by encoding a letter). In addition, when j == n, you need to reset j to 0.

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

1
  • Thank you. It works. – ruthless_g Nov 11 '15 at 7:59

You must log in to answer this question.

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