Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何重复a';对于';使用';做while';环_C_Encryption_Vigenere - Fatal编程技术网

C 如何重复a';对于';使用';做while';环

C 如何重复a';对于';使用';做while';环,c,encryption,vigenere,C,Encryption,Vigenere,这是我的维格纳密码。我试图使用do while循环在用户输入的密钥上重复迭代。该键应用于用户输入的纯文本字,一次一个字母。我需要能够迭代该键,直到明文单词的末尾,以防该键比单词短。我尝试使用do-while循环添加repeat,其中第一个for循环是关键迭代 “do while”循环的最后一行抛出未声明标识符i的错误。这是我唯一的错误。内部“for”循环来自Caesar密码,并通过了所有检查。我认为我的do while循环是错误的。如果我添加char*word[I]或word[I]的定义,不管我

这是我的维格纳密码。我试图使用do while循环在用户输入的密钥上重复迭代。该键应用于用户输入的纯文本字,一次一个字母。我需要能够迭代该键,直到明文单词的末尾,以防该键比单词短。我尝试使用do-while循环添加repeat,其中第一个for循环是关键迭代

“do while”循环的最后一行抛出未声明标识符i的错误。这是我唯一的错误。内部“for”循环来自Caesar密码,并通过了所有检查。我认为我的do while循环是错误的。如果我添加char*word[I]或word[I]的定义,不管我把它放在哪里,我都会得到一个阴影错误。我想使用这段代码,而不是完全改变它,所以我理解是否有可能这样做。不过,欢迎提出任何建议

int main(int argc, char* argv[])
{
    if (argc<2) //key
    {
        printf("Please enter your word key"); //prompts if no key is entered
        return 1;
    }

    char* key = (argv[1]);

    if(argc>=2)
    {
        printf("plaintext:");
        char* word = GetString();

        printf("ciphertext:");

        do
        {  //starts loop to repeat following for loop

            for(int l=0; l<strlen(key); l++) //iterate over letters in key
            {
                int num=l;
                for(int i=0; i<strlen(word); i++)  //iterates through word entered by user as plaintext
                {
                    if(isupper(word[i]))  //if original characters are uppercase
                    {
                        int cipher = (word[i] + num -65) % 26 + 65;
                        printf("%c", cipher);
                    }
                    else if(islower(word[i]))  //if original characters are lowercase
                    {
                        int cipher = (word[i] + num - 97) % 26 + 97;
                        printf("%c", (cipher));
                    }
                    else    //all other types of characters
                    {
                        printf("%c", word[i]);
                    }
                }
            }
            printf("\n");
        }while((word[i])<strlen(word));  // loop to recommence iterating over letters in the key   (i throwing undeclared identifier error)
    }
}
intmain(intargc,char*argv[])
{
如果(argc=2)
{
printf(“纯文本:”);
char*word=GetString();
printf(“密文:”);
做
{//启动循环,以对循环重复以下操作

对于(int l=0;l我认为你有太多的循环[级别]

如果密钥长度在单词完成之前用完,则从单词的开头(即错误)重新开始加密单词

主要目的是循环遍历所有单词字符。增加
i
的单个循环有效,前提是它也增加
l
[模键长度]

这是一个经过清理的版本[请原谅这种不必要的风格清理]:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

// NOTE: I don't have GetString on my system
char *FakeGetString(void);

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

    // key
    // prompts if no key is entered
    if (argc < 2) {
        printf("Please enter your word key");
        return 1;
    }

    char *key = (argv[1]);
    int klen = strlen(key);

    if (argc >= 2) {
        printf("plaintext:");

#if 0
        char *word = GetString();
#else
        char *word = FakeGetString();
#endif

        int wlen = strlen(word);

        printf("ciphertext:");

        // current key index
        int l = 0;

        // starts loop to repeat following for loop
        // iterates through word entered by user as plaintext
        // advance to next key char [with wrap to beginning if we're short]
        for (int i = 0;  i < wlen; ++i, l = (l + 1) % klen) {
            int num = key[l];
            int cipher;

            // if original characters are uppercase
            if (isupper(word[i])) {
                cipher = (word[i] + num - 65) % 26 + 65;
            }

            // if original characters are lowercase
            else if (islower(word[i])) {
                cipher = (word[i] + num - 97) % 26 + 97;
            }

            // all other types of characters
            else {
                cipher = word[i];
            }

            printf("%c", cipher);
        }

        printf("\n");
    }

    return 0;
}

// NOTE: I don't have GetString on my system
char *
FakeGetString(void)
{
    static char buf[1000];
    char *cp;

    fgets(buf,sizeof(buf),stdin);
    cp = strchr(buf,'\n');
    if (cp != NULL)
        *cp = 0;

    return buf;
}

更新#2:


您的代码通过了除此之外的所有检查50次::(使用“baz”作为关键字\预期输出,将“world,say hello!”加密为“xoqmd,rby gflkp!”,但不加密“ciphertext:xoqmd,szz gflkp!\n”。它没有正确加密“say”一词,这很奇怪

我使用Vigenere上Wikipedia页面的测试数据/示例对它进行了测试,但它只有一个现成的测试示例[没有空格或标点符号]

这是唯一一个包含空格(在单词“say”之前)的检查。空格必须直接复制。也许这就是原因

空格是直接复制的,所以没关系。但是

正确的方法是,复制非alpha字符时,键索引不得增加

我的版本使用
i
为短语编制索引,并使用
i%klen
为键编制索引,因此键索引将始终[有效地]递增。这就是错误所在

具有讽刺意味的是,我对此感到好奇,但当时没有扩展的测试数据

因此,解决方案是分离索引变量[再次:-)]

这是正确的版本。当我修复它时,我将
I
变量重命名为更具描述性的变量(例如
widx
),并(重新)为键创建索引变量(例如
kidx

请注意,现在,
kidx
仅在实际加密字符时递增,“pass-through”大小写不递增

#包括
#包括
#包括
//注意:我的系统上没有GetString
char*FakeGetString(void);
int
baseof(int chr)
{
int-base;
//如果原始字符是大写的
if(isupper(chr)){
基数='A';
}
//如果原始字符是小写的
else if(岛下(chr)){
基数='a';
}
//还有别的吗
其他的
基数=0;
返回基地;
}
int
main(int argc,char*argv[])
{
int-kval;
int-base;
int-widx;
int kidx;
//钥匙
//如果未输入任何键,则提示
如果(argc<2){
printf(“请输入您的word关键字”);
返回1;
}
char*key=argv[1];
int-klen=strlen(键);
//键必须是大写的,我们只需要行数
对于(kidx=0;kidx
for
循环的初始化表达式中声明了
i
,因此它仅在
for
循环中有效…这就是为什么不能在
do…while()
条件下测试它。只需在
do…while()之前声明它
而不是在
for
的初始化中。还要记住,在
for
之后,
i
将比上次通过
for
循环时的值大一个。变量
i
的范围仅限于定义它的
for
循环。在该循环之外无法访问它。如果想在
do…wh的
条件下访问
i
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// NOTE: I don't have GetString on my system
char *FakeGetString(void);

int
baseof(int chr)
{
    int base;

    // if original character is uppercase
    if (isupper(chr)) {
        base = 'A';
    }

    // if original character is lowercase
    else if (islower(chr)) {
        base = 'a';
    }

    // anything else
    else
        base = 0;

    return base;
}

int
main(int argc, char *argv[])
{
    int kval;
    int base;
    int i;

    // key
    // prompts if no key is entered
    if (argc < 2) {
        printf("Please enter your word key\n");
        return 1;
    }

    char *key = argv[1];
    int klen = strlen(key);

    // key must be uppercase and we only want row numbers
    for (i = 0;  i < klen;  ++i) {
        kval = key[i];
        base = baseof(kval);

        if (base) {
            key[i] = kval - base;
            continue;
        }

        printf("Key value must be only A-Z\n");
        return 1;
    }

    if (argc >= 2) {
        printf("plaintext:");

#if 0
        char *word = GetString();
#else
        char *word = FakeGetString();
#endif
        int wlen = strlen(word);

        printf("ciphertext:");

        // starts loop to repeat following for loop
        // iterates through word entered by user as plaintext
        // advance to next key char [with wrap to beginning if we're short]
        for (i = 0;  i < wlen;  ++i) {
            int wval = word[i];
            int cipher;

            base = baseof(wval);

            // uppercase or lowercase
            if (base) {
                kval = key[i % klen];
                cipher = ((wval - base) + kval) % 26 + base;
            }

            // all other types of characters
            else {
                cipher = wval;
            }

            printf("%c",cipher);
        }

        printf("\n");
    }

    return 0;
}

// NOTE: I don't have GetString on my system
char *
FakeGetString(void)
{
    static char buf[1000];
    char *cp;

    fgets(buf,sizeof(buf),stdin);
    cp = strchr(buf,'\n');
    if (cp != NULL)
        *cp = 0;

    return buf;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// NOTE: I don't have GetString on my system
char *FakeGetString(void);

int
baseof(int chr)
{
    int base;

    // if original character is uppercase
    if (isupper(chr)) {
        base = 'A';
    }

    // if original character is lowercase
    else if (islower(chr)) {
        base = 'a';
    }

    // anything else
    else
        base = 0;

    return base;
}

int
main(int argc, char *argv[])
{
    int kval;
    int base;
    int widx;
    int kidx;

    // key
    // prompts if no key is entered
    if (argc < 2) {
        printf("Please enter your word key\n");
        return 1;
    }

    char *key = argv[1];
    int klen = strlen(key);

    // key must be uppercase and we only want row numbers
    for (kidx = 0;  kidx < klen;  ++kidx) {
        kval = key[kidx];
        base = baseof(kval);

        if (base) {
            key[kidx] = kval - base;
            continue;
        }

        printf("Key value must be only A-Z\n");
        return 1;
    }

    if (argc < 2)
        return 1;

    printf("plaintext:");

#if 0
    char *word = GetString();
#else
    char *word = FakeGetString();
#endif
    int wlen = strlen(word);

    printf("ciphertext:");

    kidx = 0;

    // starts loop to repeat following for loop
    // iterates through word entered by user as plaintext
    // advance to next key char [with wrap to beginning if we're short]
    for (widx = 0;  widx < wlen;  ++widx) {
        int wval = word[widx];
        int cipher;

        base = baseof(wval);

        // uppercase or lowercase
        if (base) {
            kval = key[kidx];
            cipher = ((wval - base) + kval) % 26 + base;
            kidx = (kidx + 1) % klen;
        }

        // all other types of characters
        else {
            cipher = wval;
        }

        printf("%c",cipher);
    }

    printf("\n");

    return 0;
}

// NOTE: I don't have GetString on my system
char *
FakeGetString(void)
{
    static char buf[1000];
    char *cp;

    fgets(buf,sizeof(buf),stdin);
    cp = strchr(buf,'\n');
    if (cp != NULL)
        *cp = 0;

    return buf;
}