I've been working on vigenere.c for awhile but I can't get the encryption right for the life of me. This is the closest I've gotten, but I keep getting this error when I use check50:
:) vigenere.c exists
:) vigenere.c compiles
:) encrypts "a" as "a" using "a" as keyword
:( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword
\ expected output, but not "xoqmd, szz gflkp!\n"
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword
\ expected output, but not "CaQGoh\n"
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword
I can't figure out why some of it encrypts properly and some of it doesn't, or why \n is showing up at the end of some encryptions, so a nudge in the right direction would be awesome. Here's the relevant part of my code:
// declare key variable
string key = argv[1];
// get text to encrypt from user
string text = GetString();
// encryption
for (int i = 0, n = strlen(text); i < n; i++)
{
// check if alphabetical
if (isalpha(text[i]))
{
// check if lowercase
if (islower(text[i]))
{
int keyletter = key[i % strlen(key)] - 97;
int cipher = ((((text[i] - 97) + keyletter) % 26) + 97);
printf("%c", cipher);
}
// check if uppercase
if (isupper(text[i]))
{
int keyletter = key[i % strlen(key)] - 65;
int cipher = ((((text[i] - 65) + keyletter) % 26) + 65);
printf("%c", cipher);
}
}
// print non-alphabetical characters
else
printf("%c", text[i]);
}
// print new line
printf("\n");
return 0;
Thanks!