C Vigenere密码不打印

时间:2017-01-23 01:11:08

标签: c encryption vigenere

我的任务是创建一个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);


}

2 个答案:

答案 0 :(得分:1)

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

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

从标准输入读取一个字符串,等到你按下 输入。所以看起来程序被卡住了。你应该 首先打印提示,例如:

printf("Enter a string: ");

答案 1 :(得分:0)

您的Checked if HDFS is accessible from any of the segment servers [gpadmin@sdw1 ~]$hdfs dfs -ls hdfs://hdm2:8020/ 循环仅修改输入字符串的第一个字符。你需要一个循环来修改每个角色:

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();你应该检查它