Guest User

Untitled

a guest
Sep 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. int main(int argc, string argv[])
  8. {
  9. //creates alphabetical indexes for both upper and lowercased letters.
  10. string alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  11. string alphaLower = "abcdefghijklmnopqrstuvwxyz";
  12.  
  13. int cipherKey;
  14. int j = 0;
  15.  
  16. if (argc == 2)
  17. {
  18. string key = argv[1];
  19. printf("Argv: %s\n", key);
  20.  
  21. //gets input for encryption
  22. string ptext = get_string("Input: ");
  23. printf("ciphertext: ");
  24.  
  25. //loops thru each character
  26. for (int i = 0, n = strlen(ptext); i < n; i++)
  27. {
  28. //B-A-C-O-N
  29. //1-0-2-14-13
  30. printf("J: %i\n", j);
  31.  
  32. char c = ptext[i];
  33. if (isalpha(c))
  34. {
  35. //sets the cipherkey based on whether it's upper or lower case
  36. if (isupper(key[j]))
  37. {
  38. cipherKey = key[j % strlen(key)] - 65;
  39. printf("CKey: %i\n", cipherKey);
  40.  
  41. }
  42. else
  43. {
  44. cipherKey = key[j % strlen(key)] - 97;
  45. printf("CKey: %i\n", cipherKey);
  46.  
  47. }
  48. //DEFINE THE KEY
  49. //converts ASCII value to match alphabetical index based on case
  50. //applies key, then prints result
  51. if (isupper(c))
  52. {
  53. int alphaIndex = c - 65;
  54. int answer = (alphaIndex + cipherKey) % 26;
  55. printf("%c", alphaUpper[answer]);
  56. }
  57. else
  58. {
  59. int alphaIndex = c - 97;
  60. int answer = (alphaIndex + cipherKey) % 26;
  61. printf("%c", alphaLower[answer]);
  62. }
  63.  
  64. j++;
  65. }
  66. //prints if character is not a letter(i.e. commas, spaces, etc)
  67. else
  68. {
  69. printf("%c", c);
  70. }
  71. }
  72. //pages breaks at end for cleanliness
  73. printf("\n");
  74. }
  75. //Prints error if too many or invalid arguments are given.
  76. else
  77. {
  78. printf("ERROR: Invalid input! Argument must be only one number!\n");
  79. return 1;
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment