Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. //
  8. int main(int argc, string argv [])
  9. {
  10.    
  11. // Check to make sure argc is not greater than 2
  12.     if (argc != 2)
  13.     {
  14.         printf("Usage: ./caesar key\n");
  15.         return 1;
  16.     }
  17.  
  18. // Iterate through the charcherts in agrv and make sure they are all digits, if they are not print an error message and return 1
  19.  
  20.         for (int k = 0, n = strlen(argv[1]); k < n; k++)
  21.  
  22.              if (isdigit(argv[1][k]) == 0)
  23.              {
  24.                    printf("Usage: ./caesar key \n");
  25.                    return 1;
  26.               }
  27.      
  28. // Turn the string into int's
  29.  
  30.         int ato = atoi(argv[1]);
  31.    
  32. //check to make sure the key is not greater than 26
  33.  
  34.         if (ato > 26)    
  35.         {
  36.            printf("Usage: ./caesar key \n");
  37.            return 1;
  38.         }
  39.    
  40. // Get a string from the user and store in the variable called plaintext, and then print ciphertext
  41.  
  42.         string plaintext = get_string("plaintext: ");
  43.         printf("ciphertext: ");
  44.    
  45. // Iterate over in plain1 and if plain1 is less than the strlen of plaintext, print the character that plain1 will be at and add 1 to that character and also add one to plain1, print a newline and exit by returning 0.  That is if plain1 = 0, the 0th character in plaintext[plain1] will be h, if hello is enterted for plaintext.
  46.  
  47.         for (int p1 = 0; p1 < strlen(plaintext); p1++)
  48.        
  49.             if (p1 >= 65 && p1 = < 90)
  50.            
  51.                 if (plaintext[p1]  + ato > 90)
  52.                 {
  53.                     printf("%c", plaintext[p1]);
  54.                 }
  55.                
  56.                 else
  57.                 {
  58.                     printf("%c", plaintext[p1] + ato);
  59.                 }
  60.    
  61.               else if (p1 >= 97 && p1 =< 122)
  62.            
  63.                 if (plaintext[p1]  + ato > 122)
  64.                  {
  65.                     printf("%c", plaintext[p1]);
  66.                  }
  67.        
  68.                else
  69.                 {
  70.                    printf("%c", plaintext[p1] + ato);
  71.                 }
  72.        
  73.                else printf("%c", plaintext[p1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement