Advertisement
decs4usa

caesar

Oct 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. int main(int argc, string argv[1])
  8. {
  9.    
  10.     int key = atoi(argv[1]); //user input at start of program
  11.    
  12.     if (argc != 2 || key < 1) //if user doesn't run command-line argument of one or enter positive int
  13.     {
  14.         printf("Usage: ./caesar key\n"); //prompt them to run it again
  15.         return 1; //returning zero means everything went well
  16.     }
  17.    
  18.     string s = get_string("plaintext: "); //prompt for plaintext        
  19.     for (int i = 0; i < strlen(s); i++) //length of the entire string
  20.        
  21.     string cipher = [(((s[i] - 97) + key) % 26) + 97;]; //create new string, add key and rotate
  22.     for (int j = 0; j < strlen(s); j++)
  23.        
  24.         if isupper(s[i])
  25.             char c = [(((s[i] - 65) + key) % 26) + 65;]
  26.          
  27.         if islower(s[i])
  28.             char c = [(((s[i] - 97) + key) % 26) + 97;]
  29.     {
  30.         printf("ciphertext: %c\n", cipher[j]); //print new string after rotating    
  31.     }
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement