2017-01-23 64 views
0

我的任务是创建一个vigenere密码,但我的程序不打印任何东西。事情是,我不确定问题在哪里;是没有阅读的文件,是我的逻辑不正确,等等?任何帮助,我在哪里搞错赞赏。C Vigenere Cipher Not Printing

#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); 


} 

回答

0

您的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出错;你应该检查它
1

你没有看到任何输出的原因是因为出现这种情况第一:

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

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

printf("Enter a string: ");