I've been banging my head against my keyboard for a while now trying to figure out vigenere.c
I've finally got it really close, but I'm failing just one of the checks when uploading.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("You entered an inccorect number of arguments: enter one argument\n");
return 1;
}
char* key = argv[1];
int keylen = strlen(key);
for(int i = 0 ; i < keylen ; i++)
{
if(!isalpha(key[i]))
{
printf("Your key can only be letters of the alphabet");
return 1;
}
}
char* text = GetString();
for(int i = 0, j = 0, n = strlen(text) ; i<n ; i++, j++)
{
if(j >= keylen) //Resets j to 0 if j is greater than or equal to the keylength
{
j = 0;
}
if(!isalpha(text[i])) //If character is not alphabetical, subtract j by 1
{
j -= 1;
}
if(isupper(text[i]))
{
text[i] = ((((text[i] - 65) + (key[j] - 65)) % 26) + 65);
printf("%c", text[i]);
}
else if(islower(text[i]))
{
text[i] = ((((text[i] - 97) + (key[j] - 97)) % 26) + 97);
printf("%c", text[i]);
}
else
{
printf("%c", text[i]);
}
}
printf("\n");
return 0;
}
Here is the error I'm getting:
Why is BaRFoo encrypting to CaQGoh (lowercase "H" as the last character) instead of CaQGon when BaZ is the key????
If anyone could offer any insight, I would be very grateful!