0

I've been struggling with vigenere for months. please help me to figure out my errors and guide me clearly. any help much appreciated. enter image description here

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

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

string p;

if(argc != 2)   //Check if argc is 2 and returns an error if it's not
{
    printf("Please enter a key!\n");

    return 1;
}

string key = argv[1];   //Setting variable key to argv[1]

int keylen = strlen(key);

int j;

for(j = 0; j < keylen; j++)
{
    if(!isalpha(argv[1][j]))
    {
        printf("Enter an alphabetical key!\n");  //Print error message and exit

        return 1;
    }
}
    p = GetString();

    int ptextlen = strlen(p);

    int encrypt;

    int cipherk;

    int cipherp;

    for(int i = 0; i < ptextlen; i++)
    {
        if(isupper(p[i]) && isupper(key[j]))
        {
            cipherk = key[j] - 'A';

            cipherp = p[i] - 'A';

            encrypt = (cipherk + cipherp)%26 + 'A';

            printf("%c", encrypt);
        }
        else if(islower(p[i]) && islower(key[j]))
        {
            cipherk = key[j] - 'a';

            cipherp = p[i] - 'a';

            encrypt = (cipherk + cipherp)%26 + 'a';

            printf("%c", encrypt);
        }
        else if(islower(p[i]) && isupper(key[j]))
        {
            cipherk = key[j] - 'A';

            cipherp = p[i] - 'a';

            encrypt = (cipherk + cipherp)%26 + 'a';

            printf("%c", encrypt);
        }
        else if(isupper(p[i]) && islower(key[j]))
        {
            cipherk = key[j] - 'a';

            cipherp = p[j] - 'A';

            encrypt = (cipherk + cipherp)%26 + 'A';

            printf("%c", encrypt);
        }
        else
        {
            printf("%c", p[i]);
        }
    }

 printf("\n");

 return 0;
}
0

Struggling for months? wow. And so close!

Have you examined what the code actually does? Specifically, have you looked at the value of j when it encrypts a letter, and how j is initialized and incremented as the encryption for loop is executed?

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

9
  • So close ? that's a good news. i used j for the encryption because it's the j'th character of the key, so it could be taken from the for loop of the key and be used in the for loop of the encryption but now i guess the way i used j for the encryption was wrong. how can i improve this ? Oct 29 '16 at 10:07
  • I didn't say that. I asked if you had actually checked the VALUE of j at various times. THINK ABOUT THAT!!!!
    – Cliff B
    Oct 29 '16 at 10:54
  • j has no value because i haven't assigned any value to it and incremented it in my second for loop. am i right ? Oct 29 '16 at 18:40
  • The next thing you need to learn is to go ahead and test your theories. It's much easier and faster to test your ideas than to wait for a response from a forum. It's also the fastest way to develop programming skills and confidence. So, have you tested your theory yet? Did it work? ;-)
    – Cliff B
    Oct 29 '16 at 20:54
  • of course i tested it. i tested it by incrementing j in my second for loop, it actually worked and now the ciphering is taking effect. but the ciphered text is some trash text, not the correct one. imgur.com/a/Vo4uK Oct 29 '16 at 21:00

You must log in to answer this question.

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