everyone,
I have been having trouble making vigenere encipher BaRFoo correctly when the key is BaZ. I kept getting the following error: "\ expected output, but not "CaQGoh\n"," which suggests to me that the wrapping of the key is working, as is the enciphering when the text and key cases match. What seems to not be working, then, is enciphering when the text and key cases do not match. (all other tests were green smiley faces)
My first attempt was converting the key's jth character (accounting for wrapping) to the case of the corresponding text's ith character separating the text characters in two "if" groups:
// begins original code
if (isupper(text[i]) { toupper(argv[1][j]); printf("%c", ((((int) text[i] - 'A') + ((int) argv[1][j] - 'A')) % 26) + 'A'); }
if (islower(text[i]) { tolower(argv[1][j]); printf("%c", ((((int) text[i] - 'a') + ((int) argv[1][j] - 'a')) % 26) + 'a'); }
// ends original code
It seems to me that if (isupper...) {toupper...} and if (islower...) {tolower...} are not doing the trick as I expected.
Eventually I made vigenere work by removing the case convertion for the key's jth characters described above and creating four different "if" cases, two when the text and key cases match as upper- and lowercase, and two for the two possible mismatches (key is upper but text is lower, vice versa). (See code below; key validation code omitted)
This results in: 1) an overly long code, in my opinion; 2) no use of toupper and tolower, as suggested in another post; and 3) a lot of confusion on my part. It seems to me like the assumptions I was making initially (that the case of the key's jth character will be transformed if needed) do not hold, and I have no idea why.
Any pointers or suggestions will be greatly appreciated!!
//begins working code
include
include
include
include
include
int main(int argc, string argv[]) {
// //////\ // code for returning 1 if key is invalid goes here // //////\
// begins code to encipher text provided by user
// declare counter variables to move through key, return 1 if not alpha
int j = 0, n = strlen(argv[1]);
// get text to encrypt from user
string text = GetString();
//declare counter variables to move through text array
int i, x;
// encipher each text character using a corresponding key letter, until end of text is reached
for (i = 0, j = 0, x = strlen(text); i < x; i++, j = (j + 1) % n)
{
// encipher character only if it is alphabetic
if (isalpha(text[i]))
{
// if both character and key are uppercase
if (isupper(text[i]) && isupper(argv[1][j]))
{
// calculate ASCII value of uppercase letter: ((p+k-130)%26)+65
printf("%c", ((((int) text[i] - 'A') + ((int) argv[1][j] - 'A')) % 26) + 'A');
}
///if character is upper but key is lowercase
else if (isupper(text[i]) && islower(argv[1][j]))
{
//change case of corresponding key letter
//toupper(argv[1][j]);
// calculate ASCII value of uppercase letter: ((p+k-130)%26)+65
printf("%c", ((((int) text[i] - 'A') + ((int) argv[1][j] - 'a')) % 26) + 'A');
}
// if both character and key are lowercase
else if (islower(text[i]) && islower(argv[1][j]))
{
// calculate ASCII value of lowercase letter: ((p+k-194)%26)+97
printf("%c", ((((int) text[i] - 'a') + ((int) argv[1][j] - 'a')) % 26) + 'a');
}
// if character is lowercase but key is uppercase
else if (islower(text[i]) && isupper(argv[1][j]))
{
//change case of corresponding key letter
//tolower(argv[1][j]);
// calculate ASCII value of lowercase letter: ((p+k-194)%26)+97
printf("%c", ((((int) text[i] - 'a') + ((int) argv[1][j] - 'A')) % 26) + 'a');
}
}
// and print as is if character is not alphabetic
else
{
printf("%c", text[i]);
j--;
}
}
}
// carriage return at end of program
printf("\n");
return 0;
}