Advertisement
immuntasir

Caesar Cipher

Jul 26th, 2015
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. int main() {
  3.     printf("What operation do you want to execute?\n");
  4.     printf("Press 1 for Encryption.\n");
  5.     printf("Press 2 for Decryption.\n");
  6.     int i;
  7.     scanf("%d", &i);
  8.     if (i==1) {
  9.         int daanebaame, kotoghor;
  10.         printf("Forward or backward?\n");
  11.         printf("Press 1 for Forward.\n");
  12.         printf("Press 2 for Backward.\n");
  13.         scanf("%d", &daanebaame);
  14.         printf("How many positions to move?\n");
  15.         scanf("%d", &kotoghor);
  16.         // nicher kaj ta keno korchi bolo to?
  17.         if (daanebaame == 2) {
  18.             kotoghor = 26-kotoghor;
  19.         }
  20.         char plaintext[100];
  21.         char cyphertext[100];
  22.         getchar(); // eta dite hoyeche ekishathe gets ar scanf use korar karone. google korle aro jante parbe!
  23.         gets(plaintext);
  24.         int a,len = strlen(plaintext);
  25.         for (a=0; a<len;a++) {
  26.             if (plaintext[a] + kotoghor<='Z') cyphertext[a] = plaintext[a] + kotoghor;
  27.             else cyphertext[a] = 'A' + kotoghor - ('Z'-plaintext[a]);
  28.         }
  29.         cyphertext[len] = '\0';
  30.         printf("Your desired Encrypted text is:\n%s\n", cyphertext);
  31.     }
  32.     else if (i==2) {
  33.         char cypher[100], plain[100];
  34.         getchar();
  35.         printf("Cypher text please?\n");
  36.         gets(cypher);
  37.         int ghor,a,len = strlen(cypher);
  38.         for (ghor=1; ghor<=25;ghor++) {
  39.             printf("Forwarding %d position: ", ghor);
  40.             for (a=0; a<len; a++) {
  41.                 if (cypher[a] + ghor<='Z') plain[a] = cypher[a] + ghor;
  42.                 else plain[a] = 'A' + ghor - ('Z'- cypher[a]);
  43.             }
  44.             plain[len] = '\0';
  45.             printf("%s\n", plain);
  46.         }
  47.     }
  48.     else {
  49.         printf("Vai thik moto input den na ken? :'(\n");
  50.         printf("Abar notun kore shuru koren!\n\n");
  51.         // nicher kaj ta interesting na? Ki hobe bolo to? :p
  52.         main();
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement