Advertisement
Guest User

Caesar

a guest
Nov 15th, 2021
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4. #include <string.h>
  5.  
  6.  
  7. int main(int argc, string argv[])
  8. {
  9.     if(argc==2)
  10.     {
  11.         for(int j=0;j<strlen(argv[1]);j++)
  12.         {
  13.             if(argv[1][j]>='0' && argv[1][j]<='9')
  14.             {
  15.                 //printf("%c\n", argv[1][j]);
  16.             }
  17.             else
  18.             {
  19.                 printf("Only type one key.\n");
  20.                 return 1;
  21.             }
  22.         }
  23.  
  24.        int k = strtol(argv[1], NULL, 10);
  25.         //printf("%i\n", k);
  26.  
  27.         if (k>0 && k<27)
  28.         {
  29.             //printf("good key.\n");
  30.         }
  31.         else if (k>26)
  32.         {
  33.             do
  34.             {
  35.                 k = k - 26;
  36.             }
  37.             while(k>26);
  38.             //printf("key is actually %i\n", k);
  39.         }
  40.         else
  41.         {
  42.             printf("key must be positive.\n");
  43.             return 1;
  44.         }
  45.  
  46.         string plain=get_string("plaintext: ");
  47.         printf("ciphertext: ");
  48.  
  49.         for(int i=0;i<=strlen(plain);i++)
  50.         {
  51.             if (plain[i] >= 'a' && plain[i] <= 'z')
  52.             {
  53.                 int cipher= (int)plain[i] + k;
  54.                 if (cipher>122)
  55.                 {
  56.                     cipher = cipher - 26;
  57.                 }
  58.                 char c=(char)cipher;
  59.                 printf("%c",c);
  60.             }
  61.             else if (plain[i] >= 'A' && plain[i] <= 'Z')
  62.             {
  63.                 int cipher= (int)plain[i] + k;
  64.                 if (cipher>90)
  65.                 {
  66.                     cipher = cipher - 26;
  67.                 }
  68.                 char c=(char)cipher;
  69.                 printf("%c",c);
  70.             }
  71.             else
  72.             {
  73.                 int cipher= plain[i];
  74.                 char c=(char)cipher;
  75.                 printf("%c",c);
  76.             }
  77.         }
  78.         printf("\n");
  79.         return 1;
  80.     }
  81.  
  82.     else
  83.     {
  84.         printf("Usage: ./caesar key\n");
  85.         return 1;
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement