This is the cleanest way i can think of of implementing vigenere, but i don't see whats wrong.. the code should work fine.. but its not. Please help
string k = argv[1];
int keyLength = strlen(k);
int keyCodes[keyLength];
int keyCount = 0;
printf("Please enter your plaintext: \n");
string plainText = GetString();
for(int i = 0; i < keyLength; i++)
{
keyCodes[i] = toupper(k[i] - 65);
}
for(int i = 0, n = strlen(plainText); i < n; i++)
{
if(!isalpha(plainText[i]))
{
printf("%c", plainText[i]);
}
else
{
printf("%c", caesar(plainText[i], keyCodes[keyCount]));
if(keyCount < keyLength - 1)
{
keyCount++;
}
else
{
keyCount = 0;
}
}
}
printf("\n");
return 0;
}
char caesar(char currentKey, int key)
{
if(isupper(currentKey))
{
return((((currentKey - 65) + key) %26) + 65);
}
else
{
return((((currentKey - 97) + key) %26) + 97);
}
}
this is just the part that handles the ciphering
It compiles but its outputs are all wrong..
Here is the outputs:
jharvard@appliance (~/Dropbox/workspace/pset2): ./vigenere bob Please enter your plaintext: foo bar plaintext miv iuy wfhphalra
But the output should be :gcp cos qzbjbuflu