currently doing the vigenere code & have gotten the green light on all but one of the checks. The check it is failing is the case when the key is 'BaZ' and the word is 'CaQGon'.
The message given is;
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "ciphertext: C=gQKGuhn\n"
i've also tried my key to be 'aBc' & the word to be encrypted be 'abc' & the output given is 'ag[ek' , my code works fine if the k value is all lower case letters or all upper case letters - it's more so if the k value is a mix of upper and lower case letters.
I've tried to define cases where the code word is lower case and the key is upper & vice versa, but have been unsuccessful in fixing this problem! Any tips as to where the code needs to be touched would be much appreciated!
The code is listed belooooow.
Cheers.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// code is the string the user will be inputting
string k;
string code;
k = argv[1];
// case where only one argument is put in command line!
if (argc == 1)
{
code = "need dat code value in the command line";
k = ".";
printf("%s%s\n", code, k);
return 1;
}
// case where more than two arguments are put in the command line!
if (argc > 2)
{
code = "too many argssssss";
k = ".";
printf("%s%s\n", code, k);
return 1;
}
// correct amount of arguments
if (argc == 2)
{
// checking if string inputted is a word
for (int p = 0; p < strlen(argv[1]); p++)
{
if (!isalpha(argv[1][p]))
{
printf("not a word!\n");
return 1;
}
}
// getting codeword input from user
printf("plaintext: ");
code = get_string();
// defining a few arrays which will help with encryption
int l = 0;
int j = 0;
int kl;
kl = strlen(k);
int ncode[strlen(code)];
int num[strlen(code)];
printf("ciphertext: ");
// loop to get shiz going
for (int i = 0, n = strlen(code); i < n; i++)
{
// for code letter being an uppercase letter
if (isupper(code[i]))
{
if (isupper(argv[1][(l - j)%kl]))
{
ncode[i] = code[i] - 'A';
num[i] = ((ncode[i] + (argv[1][(i - j)%kl] - 'A')))%26;
printf("%c", num[i] + 'A');
l++;
}
if (islower(argv[1][(l - j)%kl]))
{
ncode[i] = code[i] - 'A';
num[i] = (ncode[i] + (argv[1][(i - j)%kl] - 'a'))%26;
printf("%c", num[i] + 'A');
l++;
}
}
// for code letter being a lowercase letter
if (islower(code[i]))
{
if (islower(argv[1][(l - j)%kl]))
{
ncode[i] = code[i] - 'a';
num[i] = ((ncode[i] + (argv[1][(i - j)%kl] - 'a')))%26;
printf("%c", (num[i] + 'a'));
l++;
}
if (isupper(argv[1][(l - j)%kl]))
{
ncode[i] = code[i] - 'a';
num[i] = (ncode[i] + (argv[1][(i - j)%kl] - 'A'))%26;
printf("%c", (num[i] + 'a'));
l++;
}
}
if (code[i] < 'A' || (code[i] > 'Z' && code[i] < 'a') || code [i] > 'z')
{
printf("%c", code[i]);
j++;
l++;
}
}
printf("\n");
}
}