Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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 shift(char q);// declaring function to handle keyword
  9.  
  10. int main(int argc, string argv [])
  11.  
  12. {
  13. string j = argv [1] ;
  14.  
  15. if (argc != 2)
  16. {
  17. printf("BAD COMMAND\n");
  18. return 1;
  19. }
  20.  
  21. for (int s = 0, m = strlen(j); s < m; s++) // to iterate over keyword string
  22.  
  23. {
  24. if ( isalpha (j[s])== false)
  25. {
  26.  
  27. printf("BAD COMMAND\n");
  28. return 1;
  29. }
  30.  
  31. else
  32. {
  33.  
  34. ;
  35.  
  36. }
  37.  
  38. }
  39.  
  40. char k[strlen(j)] ; // declaring array k
  41.  
  42.  
  43. string p = get_string("plaintext: ");
  44.  
  45. char c [strlen(p)]; // a char variable with the same string length of p
  46.  
  47. printf("ciphertext: ");
  48. int x ; // variable to preserve letter case
  49.  
  50. int u = 0;
  51.  
  52.  
  53. for (int i = 0, n = strlen(p) ; i < n; i++) //this is the loop to iterate over each character of the string
  54.  
  55. {
  56. k[u] = shift(argv [1][u]);
  57.  
  58. if (isalpha (p[i]) == true)
  59.  
  60. {
  61. u++ ;
  62. }
  63.  
  64. if (isupper(p[i]))
  65. {
  66. x = 65;
  67. }
  68. else if (islower(p[i]))
  69. {
  70. x = 97;
  71. }
  72.  
  73.  
  74. c[i] = (((p[i] - x) + k[u]) % 26) + x ; // this equation is to convert from ascii to alphabet index, as well as plaintext to ciphertext
  75.  
  76. printf("%i", k[u]);
  77.  
  78.  
  79. if (isalpha(p[i]))
  80. {
  81.  
  82. printf("%c", c[i]);
  83. }
  84. else if (p[i] == 32)
  85. {
  86. printf(" "); // to leave spaces as they are
  87. }
  88.  
  89. else
  90. {
  91. printf("%c", (p[i])); //to leave non alphaetic characters as they are
  92. }
  93. }
  94.  
  95. printf("\n");
  96.  
  97. }
  98.  
  99. int shift(char q) // defining shift function
  100.  
  101. {
  102.  
  103. int keyword;
  104.  
  105.  
  106. if (islower(q))
  107. {
  108.  
  109. keyword = ((int) (q)) -97;
  110. }
  111.  
  112. else
  113. {
  114. keyword = ((int) (q)) -65;
  115. }
  116.  
  117. return keyword;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement