I've been struggling with vigenere for months. please help me to figure out my errors and guide me clearly. any help much appreciated.
#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;
}