Advertisement
theguyoveryonder

Untitled

Nov 23rd, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. char menu(void);
  5. char action(char option);
  6. void encypt();
  7. void decrypt();
  8.  
  9.  
  10.  
  11. char menu(void){
  12.   int i;
  13.   char option;
  14.   printf("Pick whether you would like to enycrypt a text or decrypt a text.\n");
  15.   printf("1 - encrypt\n");
  16.   printf("2 - decrypt\n");
  17.   printf("x - end\n");
  18.   scanf(" %c", &option);
  19.  
  20.  
  21.  
  22.   return option;
  23. }
  24.  
  25.  
  26. void encrypt(){
  27.   char message[100], ch;
  28.     int i, key;
  29.    
  30.     printf("Enter a message to encrypt: \n");
  31.     fgets(message,100,stdin);
  32.     scanf("%s",message);
  33.     printf("Enter key: \n");
  34.     scanf("%d", &key);
  35.    
  36.     for(i = 0; message[i] != '\0'; ++i){
  37.         ch = message[i];
  38.        
  39.         if(ch >= 'a' && ch <= 'z'){
  40.             ch = ch + key;
  41.            
  42.             if(ch > 'z'){
  43.                 ch = ch - 'z' + 'a' - 1;
  44.             }
  45.            
  46.             message[i] = ch;
  47.         }
  48.         else if(ch >= 'A' && ch <= 'Z'){
  49.             ch = ch + key;
  50.            
  51.             if(ch > 'Z'){
  52.                 ch = ch - 'Z' + 'A' - 1;
  53.             }
  54.            
  55.             message[i] = ch;
  56.         }
  57.     }
  58.    
  59.    printf("Encrypted message: %s\n", message);
  60. }
  61. void decrypt(){
  62.   char ch,message_1[100];
  63.     int i, key;
  64.   printf("Enter a message to decrypt:\n");
  65.     fgets(message_1,100,stdin);
  66.     scanf("%s",message_1);
  67.      printf("Enter key: ");
  68.     scanf("%d", &key);
  69.     for(i = 0; message_1[i] != '\0'; ++i){
  70.         ch = message_1[i];
  71.        
  72.         if(ch >= 'a' && ch <= 'z'){
  73.             ch = ch - key;
  74.            
  75.             if(ch < 'a'){
  76.                 ch = ch + 'z' - 'a' + 1;
  77.             }
  78.            
  79.             message_1[i] = ch;
  80.         }
  81.         else if(ch >= 'A' && ch <= 'Z'){
  82.             ch = ch - key;
  83.            
  84.             if(ch < 'A'){
  85.                 ch = ch + 'Z' - 'A' + 1;
  86.             }
  87.            
  88.             message_1[i] = ch;
  89.         }
  90.     }
  91.    
  92.     printf("Decrypted message: %s\n", message_1);
  93. }
  94. int main(){
  95.     char option;
  96.     printf("Encryption and decription program\n");
  97.     printf("-------------------------------------\n");
  98.    
  99.     do{
  100.       option = menu();
  101.       switch(option){
  102.          case '1':
  103.       encrypt();
  104.       break;
  105.  
  106.       case '2':
  107.       decrypt();
  108.       break;
  109.      
  110.       }
  111.       getchar();
  112.     }
  113.     while(option != 'x');
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement