Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. //i is what number character of the string, s asks you for a string, n asks you what the key is, and b is what will be printed
  7. // lines 8 to 14 makes sure the user puts in 2 command line arguments
  8. // int a is what number character of the cipherc code
  9.  
  10. int main(int argc, string argv[])
  11. {
  12.     //making sure I have two command line arguments
  13.     if (argc != 2)
  14.  
  15.     {
  16.         printf("Usage:%s abc\n" , argv[0]);
  17.         return 1;
  18.     }
  19.     //code only runs with two command line arguments
  20.     if (argc == 2)
  21.     {
  22.         string s = get_string("plaintext: ");
  23.         string b = (argv[1]);//argv1 is the string given at the command line argument
  24.         int a = 0;
  25.  
  26.  
  27.  
  28.  
  29.         // setting the cipher to the correct amount
  30.  
  31.             for (int i = 0; i < strlen(s); i++)
  32.  
  33.  
  34.             {
  35.                 if (i < strlen(b))
  36.                 {
  37.                     a = i;
  38.                     b[a] = atoi(b[a]);
  39.  
  40.                 }
  41.                 else
  42.                 {
  43.                     a = (i - strlen(b));
  44.                 }
  45.  
  46.                 {
  47.  
  48.  
  49.                     if (islower(b[a]))
  50.                     {
  51.                         b[a] = toupper(b[a]);
  52.                     }
  53.                     if (isupper(b[a]))
  54.                     {
  55.                         b[a] = b[a] - 65;
  56.  
  57.                     }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.                             // making sure the code wraps around capital z instead of going over it
  65.                             while (s[i] + b[a] > (int)'Z' && isupper(s[i]))
  66.  
  67.                             {
  68.  
  69.                                  s[i] = s[i] - 26;
  70.                             }
  71.  
  72.                                 if  isspace(s[i])
  73.                                 {
  74.                                     printf(" ");
  75.                                     i = i + 1;
  76.  
  77.                                 }
  78.                                 if ispunct(s[i])
  79.                                 {
  80.                                     s[i] = s[i] - b[a];
  81.                                 }
  82.  
  83.  
  84.  
  85.                                 // making sure the code wraps around lowercase z instead of going over it
  86.  
  87.                                     while (s[i] + b[a] > (int)'z' && islower(s[i]))
  88.  
  89.                                     {
  90.  
  91.                                         s[i] = s[i] - 26;
  92.                                     }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.                                 printf("%c", s[i] + b[a]);
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.                 }
  119.         }
  120.  
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement