Took me awhile to figure out the logic behind this project. I thought I had it all worked out but I keep failing one check.
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQGoh\n"
My code is below. Can someone please help me understand where my mistake is?
#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("Invalid input!!! Requires a \"String\" as a command line parameter\n");
return 1;
}
else
{
for (int i = 0, s=strlen(argv[1]); i < s; i++)
{
if (!isalpha(argv[1][i]))
{
printf("You must enter letters only for your key\n");
return 1;
}
}
}
string key = argv[1];
char* pltxt = GetString();
int key_index = 0;
int j = 0;
for (int i=0, n =strlen(pltxt); i<n; i++)
{
if (isalpha(pltxt[i]))
{
{
key_index = j % strlen(key);
}
if islower(pltxt[i])
{
int a = ((pltxt[i] -97 +key[key_index] -97 ) %26 + 97);
printf("%c", a);
}
else if isupper(pltxt[i])
{
int b = ((pltxt[i] -65 +key[key_index] - 65 ) %26 + 65);
printf("%c", b);
}
j++;
}
else
{
printf("%c", pltxt[i]);
}
}
printf("\n");
return 0;
}