VigénereCipherin C

问题描述 投票:0回答:2
#include <stdio.h>
#include <string.h> // strlen()
#include <ctype.h> // isupper() , tolower()

void vigenereCipher(char* plainText, char* key);

int main(int argc, char argv[])
{
    char* key = argv[1];
    char plainText[101];

    // Ask the user for a sentence/word to encrypt
    printf("Please enter a word or sentence: ");
    fgets(plainText, sizeof(plainText), stdin);

    // Print the used encryption key
    printf("Your encryption key: %s\n", key);

    // Print the encrypted plaintext
    printf("Your encrypted message is: ");
    vigenereCipher(plainText, key);

    return 0;
}
void vigenereCipher(char* plainText, char* key)
{

    int i;
    char cipher;
    char cipherValue;
    int len = strlen(key);

    // Loop through the length of the plainText string
    for (i = 0; i < strlen(plainText); i++)
    {

        if (islower(plainText[i]))
        {
            cipherValue = ((int)plainText[i] - 97 + (int)tolower(key[i % len]) - 97) % 26 + 97;
            cipher = (char)cipherValue;
        }
        else
        {
            cipherValue = ((int)plainText[i] - 65 + (int)toupper(key[i % len]) - 65) % 26 + 65;
            cipher = (char)cipherValue;
        }

        // Print the ciphered character if it is alpha numeric
        if (isalpha(plainText[i]))
        {
            printf("%c", cipher);
        }
        else
        {
            printf("%c", plainText[i]);
        }

    }

}

vigenere.c:7:5:错误:'main'(参数数组)的第二个参数必须是'char **'类型int main(int argc,char argv [])^ vigenere.c:10:15:error :不兼容的整数到指针转换使用'char'类型的表达式初始化'char';用&[-Werror,-Wint-conversion] char key = argv [1]取地址;生成了^ ~~~~~~~&2错误。

我的目标是将程序的encryption密钥作为程序的参数提供,但是上面有两个错误,不知道从哪里开始。有任何想法吗? (代码片段结尾)

这是针对CS50项目的。

c encryption cs50 vigenere
2个回答
1
投票

main()的标准签名是一个字符指针数组:

int main(int argc, char* argv[])

或者,如果您更喜欢指向其他指针的字符:

int main(int argc, char** argv)

你拥有的是一系列人物。


1
投票

你在*错过了一个星号mainmain的第二个参数是char指针的数组:char *argv[]

请注意,当数组在传递给函数时衰减为指针,将第二个参数写为:

char **argv

所以,你的main()应该是:

int main(int argc, char *argv[])
{
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.