I've been stuck on Vigenere for a while now. I believe my code shifts the keyword correctly, but I am having trouble shifting the plaintext by that keyword. I feel like I'm missing something major in the code for the wrap around.
Any help is appreciated.
Thanks
int main(int argc, string argv[])
{
if (argc == 2)
{
string k = argv[1];
for (int i = 0 , m = strlen(k); i < m; i++)
{
if (isalpha(k[i]) && isupper(k[i]))
{
k[i] = (k[i] - 65);
}
else if (isalpha(k[i]) && islower(k[i]))
{
k[i] = (k[i] - 97);
}
else
{
return 1;
}
}
string p = get_string("plaintext: ");
string c = p;
int m = strlen(k);
int j = 0;
for (int i = 0; i < strlen(p); i++)
{
if (isupper(p[i]) && isalpha(p[i]))
{
c[i] = p[i] - 65;
c[i] = (p[i] + k[j % m]) % 26;
c[i] = c[i] + 65;
j++;
}
else if ( islower(p[i]) && isalpha(p[i]))
{
c[i] = p[i] - 97;
c[i] = (p[i] + k[j % m]) % 26;
c[i] = c[i] + 97;
j++;
}
}
printf("ciphertext: %s\n", c);
}
else
{
printf("error!\n");
return 1;
}
}