Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user

Vigenere fails check50 testencryptstest encrypts "BaRFoo" as "CaQGon" using "BaZ" error

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Source Link

Vigenere fails check50 testencrypts "BaRFoo" as "CaQGon" using "BaZ" error

Vigenere fails check50 test encrypts "BaRFoo" as "CaQGon" using "BaZ" error, my program output is "caQGoh".
I don't know what's the problem but i guess that this problem apears when there are different letter cases(lowercase and uppercase)plus the variance is about 6 shifts.
P.s : I am kind of new to programing and I am trying my best to use function.

Thanks for help :D

//this is my code
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>


string getChars(string plaintext, string keyword)
{
int txtlen = strlen(plaintext);
int letter;
int j = 0;
for(int i = 0; i < txtlen; i++) 
{
    letter = plaintext[i];

   // check if it's a letter 
    if (isalpha(letter)) 
    {
    // encrypt if letter
        encryptChar(letter, keyword, j);
        j++;
    } 
    // if not just print it
    else 
    {
        printf("%c", letter);
    } 
}

printf("\n");
return 0;
}

char encryptChar(int letter, string keyword, int j)
{
int indexStart;
if (isupper(letter)) 
{
    indexStart = 65;
}

else 
{
    indexStart = 97;
}

char encrypted;
int keyLen = strlen(keyword);
//I guess down here is my problem.
int LtrNum = 0;
if (isupper(letter))
{
    LtrNum = keyword[j % keyLen] - 'A';
}
else if (islower(letter))
{
    LtrNum = keyword[j % keyLen] - 'a';
}
LtrNum = (((letter - indexStart) + LtrNum) % 26);
encrypted = LtrNum + indexStart;

printf("%c", encrypted);

return 0;
}
int main(int argc, string argv[])
{
string keyword = argv[1];
string plaintext = GetString();

// pass the text and the keyword to encrypt  
getChars(plaintext, keyword);

return 0;
}`