繁体   English   中英

C Vigenere密码无法打印

[英]C Vigenere Cipher Not Printing

我的任务是创建vigenere密码,但是我的程序没有输出任何内容。 问题是,我不确定问题出在哪里。 文件没有读入,我的逻辑不正确等等吗? 任何帮助我弄乱的地方都表示赞赏。

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

void encrypt(char *theString, int shift)
{

    if (isalpha(*theString) && isupper(*theString))
    {

            *theString += shift;
            if (*theString >= 'Z')
            {
                    *theString = *theString - 26;
            }


    }

    theString++;

}


int main(void)
{

    FILE *inputFile;
    char KEY[256];
    char theString[80];
    int shift;
    int i;

    inputFile = fopen("code.txt", "r");
    if (inputFile == NULL)
    {
            printf("Failed to open\n");

            return(0);

    }
    fgets(theString, sizeof(theString), stdin);

                   printf("Enter the Key: ");
    fgets(KEY, sizeof(KEY), stdin);
    for (i = 0; i < 256; i++)
    {

            shift = KEY[i] - 65;
            encrypt(theString,shift);
            puts(theString);
    }
    return(0);


}

您没有看到任何输出的原因是因为这首先发生:

fgets(theString, sizeof(theString), stdin);

它将从标准输入中读取一个字符串,然后等待您按Enter 因此,该程序似乎卡住了。 您应该首先打印一个提示,例如:

printf("Enter a string: ");

您的encrypt循环仅修改输入字符串的第一个字符。 您需要一个循环来修改每个字符:

void encrypt(char *theString, int shift)
{
    for ( ; *theString != '\0'; theString++)
    {
        if (isupper(*theString))
        {
            *theString += shift;
            if (*theString >= 'Z')
            {
                *theString = *theString - 26;
            }

        }
    }
}

其他要点:

  • isupper()暗示isalpha() ; 两者都不需要
  • fgets()在错误时返回NULL 你应该检查一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM