2014-04-01 53 views
-1

我已經從here獲取了一個簡單的凱撒密碼的代碼,我修改了它,以便用戶定義密碼密鑰。但每次我嘗試運行它時,程序都會崩潰。凱撒密碼簡單程序

#include <stdio.h> 

int main() 
{ 
    char array[100], cipher[100]; 
    int c=0, x=0, y=0; 
    int z; 
    printf("This Program will encrypt according to your needs\n"); 
    printf("Enter the cipher key\n"); 
    scanf("%d",&z); 
    printf("Enter the sentence"); 
    while((c=getchar()) != '\n') 
    { 
     array[x++]=(char)c; 
     cipher[y++]=(char)(c+z); 
    } 

    array[x]=0; 
    cipher[y]=0; 

    printf("%s\n",cipher); 

    return 0; 
} 
+6

如果它崩潰使用調試器,曾經有一次我使用日誌語句進行調試......通過花費一個小時學習如何使用調試器可以節省的時間......我很笨,你d不一定非要。 –

+0

你的代碼有什麼問題?我試過了,它不會崩潰 – WileTheCoyot

回答

5

它不會崩潰。 while循環結束瞬間,因爲「\ n」是在scanf後輸入緩衝區,這會被讀第一

-2
//Caesar cipher program in C.  

#include<stdio.h> 
#include<string.h> 
void main() 
{ 
     int key,i; 
     char data[200]; 

     printf("\nEnter the alphabetical plain text in lowercase to be encrypted:\n"); 
     gets(data); //plain text. 

     printf("\nEnter the key value,e.g.:3,4,5,etc. : "); 
     scanf("%d",&key); //key. 

     if(key>26) 
     key=key % 26; //0<=key<26. 

     for(i=0;i<strlen(data);i++) 
     {  
     if(data[i]==' ') 
     { 
      continue; 
     } 
     else 
     { 
      if(data[i]>= 'x') 
      { 
      data[i]=data[i]-26; 
      } 

      data[i]=data[i]+key; 
      } 
    } 
     printf("Your cipher text is: %s\n",data); //cipher text. 

    } 
+2

請添加一些註釋來回答問題並幫助理解代碼 – janisz

+0

在C中執行凱撒密碼。 –

+0

注意:'if(key> 26)key = key%26; // 0 <= key <26.'如果'key == 26'或負數不會導致'0 <= key <26'。 – chux

-2
#include <stdio.h> 
#include <conio.h> 

void main() 
    { 
    int i, c; 
    char str[100]; 

    printf("Enter the Text Message : "); 
    gets(str); 

    for (i = 0; i < strlen(str); i++) 
     { 
     switch (str[i]) 
      { 
      case 'x': 
       str[i] = 'a'; 
       continue; 
      case 'y': 
       str[i] = 'b'; 
       continue; 
      case 'z': 
       str[i] = 'c'; 
       continue; 
      case 'X': 
       str[i] = 'A'; 
       continue; 
      case 'Y': 
       str[i] = 'B'; 
       continue; 
      case 'Z': 
       str[i] = 'C'; 
       continue; 
      } 

     if (str[i] >= 'a' && str[i] < 'x') 
      str[i] = str[i] + 3; 
     else if (str[i] >= 'A' && str[i] < 'X') 
      str[i] = str[i] + 3; 
     } 

    printf("Message After Encryption : \n"); 
    puts(str); 
    for (i = 0; i < strlen(str); i++) 
     { 
     switch (str[i]) 
      { 
      case 'a': 
       str[i] = 'x'; 
       continue; 
      case 'b': 
       str[i] = 'y'; 
       continue; 
      case 'c': 
       str[i] = 'z'; 
       continue; 
      case 'A': 
       str[i] = 'X'; 
       continue; 
      case 'B': 
       str[i] = 'Y'; 
       continue; 
      case 'C': 
       str[i] = 'Z'; 
       continue; 
      } 

     if (str[i] >= 'd' && str[i] <= 'z') 
      str[i] = str[i] - 3; 
     else if (str[i] >= 'D' && str[i] < 'Z') 
      str[i] = str[i] - 3; 

     } 
    printf("Message After Decryption : \n"); 
    puts(str); 
    getch(); 
    } 
+2

請格式化您的代碼並添加一個短語來解釋如何回答這個問題。 –

-2
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 

#define MAXSIZE 1024 

int main(int argc, char* argv[]) 
{ 
    char in[MAXSIZE]; 
    char en[MAXSIZE] = {0}; 

    //Input any Sting to encrypt in upper case or lower case 
    //or also mixed-up in any case. 
    read(STDIN_FILENO, in, MAXSIZE); 
    encrypt(in, en);  
    printf("%s\n%s\n", in, en); 
    bzero(in, strlen(in)); 
    decrypt(en, in); 
    printf("%s\n%s\n", en, in); 
    return 0; 
} 

int encrypt(char* input, char* cipher) 
{ 
    int i; 
    for(i = 0; i < strlen(input); i++) 
    { 
     if(input[i] >= 97 && input[i] <= 122) 
     { 
      cipher[i] = input[i]+23 <= 122 ? input[i] + 23 : input[i] - 3; 
     }else if(input[i] >= 65 && input[i] <= 90) 
     { 
      cipher[i] = input[i]+23 <= 90 ? input[i] + 23 : input[i] - 3; 
     }else 
      cipher[i] = input[i]; 
    } 
    return 0; 
} 

int decrypt(char* cipher, char* output) 
{ 
    int i; 
    for(i = 0; i < strlen(cipher); i++) 
    { 
     if(cipher[i] >= 97 && cipher[i] <= 122) 
     { 
      output[i] = cipher[i]-23 >= 97 ? cipher[i] - 23 : cipher[i] + 3; 
     }else if(cipher[i] >= 65 && cipher[i] <= 90) 
     { 
      output[i] = cipher[i]-23 >= 65 ? cipher[i] - 23 : cipher[i] + 3; 
     }else 
      output[i] = cipher[i]; 
    } 
    return 0; 
} 
+1

沒有解釋的代碼是非常有價值的。 –

0

scanf,在關鍵讀樹葉在輸入緩衝器中的換行符。然後,當您第一次撥打getchar時,它將返回\n,因此永遠不會輸入while循環。

你實際上沒有崩潰,但從來沒有機會輸入字符串進行編碼。

您需要scanf之後,但在循環之前使用額外添加到getchar消費換行符:

scanf("%d",&z); 
getchar(); 
-2

這裏是下caeser完整的代碼加密

#include<stdio.h> 
#include<conio.h> 
#include<string.h> 
#include<stdlib.h> 
int main() 
{ 
    int i,key; 
    char plain[100],cipher[100]; 
    printf("Enter key:"); 
    scanf("%d",&key); 
    key=key%26; // adjusting key 
    fflush(stdin); 
    printf("Enter text:"); 
    gets(plain); 
    for(i=0;i<strlen(plain);i++) 
    { 
     if(isalpha(plain[i])) 
     { 
      if(islower(plain[i])) 
       cipher[i]=(plain[i]+key-'a')%26+'a'; 
      else 
       cipher[i]=(plain[i]+key-'A')%26+'A'; 
     } 
     else 
      cipher[i]=plain[i]; 
    } 
    cipher[i]='\0'; 
    printf("After ciphering: %s",cipher); 
    getch(); 
    return 0; 
} 
-1
#include<stdio.h> 
#include<conio.h> 
#include<string.h> 
#include<ctype.h> 
void main() 
{ 
    char p[20]; 
    int key,i,enc; 
    clrscr(); 
    printf("Enter Plain text="); 
    gets(p); 
    printf("\n Enter Key="); 
    scanf("%d",&key); 

    for(i=0;i<strlen(p);i++) 
    { 
      p[i]=tolower(p[i]); 
      enc=((p[i]-97)+key)%26; 
      printf("%c",enc+97); 
    } 
} 
+0

請提供解釋。我可以看到用戶返回0表示程序已經完成並退出程序,只有getch()方法調用的不同之處在於等待用戶輸入任何輸入內容,如果這是您想要解釋的內容,請讓我們知道它是如何產生變化的。 – Jeet