-1

Vigenere は check50 テストに失敗し、"BaZ" を使用して "BaRFoo" を "CaQGon" として暗号化します > エラー、私のプログラム出力は "caQGoh" です。

何が問題なのかわかりませんが、この問題は、大文字と小文字の違いに加えて、分散が約 6 シフトである場合に発生すると思います。

//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;  
}  
4

1 に答える 1