Guest User

Untitled

a guest
Nov 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. // take in command line argument & eliminate any wrong intake (lines 12-14)
  8.  
  9. int main(int argc, string argv[]) {
  10.  
  11. if (argc != 2){
  12. printf ("mising command-line arguement\n");
  13. return 1;
  14.  
  15. } else {
  16.  
  17.  
  18. // check if argc[1] is a string
  19.  
  20. string key = (argv[1]);
  21.  
  22. int keylen = strlen(key);
  23.  
  24. //inside the for loop;
  25. // 1) checking if the input is solely aplha
  26. // 2) changeing key to all lowercase
  27.  
  28. for (int str = 0; str < keylen; str++){
  29.  
  30. if (!isalpha(key[str])) {
  31.  
  32. printf("Key needs to be only characters!\n");
  33. return 1;
  34. }
  35.  
  36. if ((isupper(key[str]))) {
  37. key[str] = (tolower(key[str]));
  38. }
  39.  
  40. }
  41.  
  42. // now key is in lowercase
  43. // will have to use isalpha when iterating over plaintext
  44.  
  45. //take in plaintext and display ciphertext;
  46.  
  47. printf("Plaintext: ");
  48. string plaintext = get_string();
  49. printf("Ciphertext: ");
  50.  
  51. // now to iterate over the plaintext
  52.  
  53. int g = 0;
  54.  
  55. for (int q = 0; q < strlen(plaintext) ; q++){
  56.  
  57.  
  58. //need to ensure that its only letters that change using ASCII values
  59.  
  60. int l = (g % (strlen(argv[1])));
  61.  
  62.  
  63. if islower(plaintext[q]) {
  64.  
  65. printf("%c", ((((plaintext[q] - 97) + (key[l] - 97)) %26 ) +97 ));
  66. g++;
  67.  
  68. }
  69.  
  70. else if isupper(plaintext[q]) {
  71. printf("%c", ((((plaintext[q] -65) + (key[l] - 65)) %26 ) +65 ));
  72. g++;
  73.  
  74. }
  75.  
  76. else {
  77. printf("%c", (plaintext[q]));
  78. }
  79.  
  80. }
  81.  
  82. printf("\n");
  83.  
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment