Facebook
From Andrew Tan, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 123
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <cs50.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7.  
  8. const int A = 26;
  9.  
  10. int main(int argc, string argv[])
  11. {
  12.     //checking if one command-line argument is given
  13.     if (argc != 2)
  14.     {
  15.         printf("Usage: ./caesar key\n");
  16.         return 1;
  17.     }
  18.    
  19.     else
  20.     {
  21.         //testing if key consists of proper positive integers
  22.         for (int i = 0, n = strlen(argv[1]); i < n ;i++)
  23.         {
  24.             if (isdigit(argv[1][i]) == 0)
  25.             {
  26.                 return 1;
  27.             }
  28.        
  29.         }
  30.  
  31.     }
  32.    
  33.     string message = get_string("plaintext: ");
  34.     int key = atoi(argv[1]);
  35.     printf("ciphertext: ");
  36.     for (int i = 0, n = strlen(message); i < n; i++)
  37.     {
  38.         string c;
  39.         printf("%c", message[i] + key);
  40.     }
  41. printf("\n");    
  42. }