Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. int main(int argc, string argv[])
  7. {
  8.     while (argc!=2){
  9.         printf("Usage: ./caesar key\n");
  10.         return 1;
  11.     }
  12.     for (int w = 0; w<strlen(argv[1]); w++){
  13.          char d = argv[1][w];
  14.          if(d<48 || d>57)
  15.          {
  16.              printf("Usage: ./caesar key\n");
  17.              return 1;
  18.          }
  19.     }
  20.     int k = atoi(argv[1]);
  21.    
  22.     string a = get_string("plaintext: ");
  23.     printf("ciphertext: ");
  24.     for (int i = 0, n = strlen(a); i < n; i++)
  25.     {
  26.         char p = a[i];
  27.        
  28.         if (a[i] >= 'a' && a[i] <= 'z')
  29.         {
  30.             char c = p + k;
  31.             if (c > 'z')
  32.             {
  33.                 c=(c-'z')+('a'-1);
  34.             }
  35.             printf("%c", c);
  36.         }
  37.        else if (a[i] >= 'A' && a[i] <= 'Z')
  38.         {
  39.             char c = p + k;
  40.             if (c > 'Z')
  41.             {
  42.                 c=(c-'Z')+('A'-1);
  43.             }
  44.             printf("%c", c);
  45.         }
  46.         else {
  47.             printf("%c", a[i]);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement