Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(int argc, string argv[])
  7. {
  8.     // Print error message if more/less than one command line argument is given
  9.     if (argc != 2)
  10.     {
  11.         printf("Error\n");
  12.         return 1;
  13.     }
  14.  
  15.     // If one command line argument is given, turn the string into an int to give key k
  16.     int k = atoi(argv[1]);
  17.  
  18.     // Error message k is a negative integer
  19.     if (k < 0)
  20.     {
  21.         printf("Error\n");
  22.         return 1;
  23.     }
  24.     else
  25.     {
  26.         // Prompt user for the plaintext string
  27.         printf("plaintext:  ");
  28.         string s = get_string();
  29.  
  30.         // Transform plaintext string by size 'k'
  31.         printf("ciphertext: ");
  32.         for (int i = 0, n = strlen(s); i < n; i++)
  33.         {
  34.             if (isupper(s[i]))
  35.             {
  36.                 printf("%c", (((s[i] + k) - 65) % 26) + 65);
  37.             }
  38.             else if (islower(s[i]))
  39.             {
  40.                 printf("%c", (((s[i] + k) - 97) % 26) + 97);
  41.             }
  42.             else
  43.             {
  44.                 printf("%c", s[i]);
  45.             }
  46.         }
  47.     }
  48.     printf("\n");
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement