2017-01-23 75 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: ");