This is my first time asking a question here. I did read through quite a few answers but I'm still stuck.
My code compiles, but the generated ciphertext is not correct.
For example, for the keyword "bacon" and plaintext "Meet me at the park at eleven am" the output is "ciphertext: Pgiu me at tjg tarn dt eofzfp en". It should be "ciphertext: Negh zf av huf pcfx bt gzrwep oz".
I'm 99% sure my issue is with how I used the modulo for how it cycles through the key word.
If anyone could offer any insight I would be very appreciative.
//Vigenere cipher created for CS50 intro to CS course
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
//Only accept one key word. No more no less.
if (argc !=2)
{
printf("Gimmie ONE key\n");
return 1;
}
//No numbers allowed in key word
else
{
for (int i = 0, n = strlen(argv[1]); i < n; i++)
{
if (isdigit(argv[1][i]))
{
printf("No numbers! Geez!\n");
return 1;
}
}
}
string k = argv[1];
string p = get_string("Input plaintext: ");
printf("ciphertext: ");
for (int i = 0, j = 0, n = strlen(p), klength = strlen(k); i < n; i++)
{
if (isalpha(p[i]))
{
if (islower(p[i]))
{
printf("%c", (((p[i] + k[j] % klength) - 97) % 26) + 97);
}
else
{
printf("%c", (((p[i] + k[j] % klength) - 65) % 26) + 65);
}
j++;
}
else
{
printf("%c", p[i]);
}
}
printf("\n");
}