Advertisement
Guest User

Untitled

a guest
Jul 9th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6.  
  7. //elements used for the encrypt function
  8. string abcArray = "abcdefghijklmnopqrstuvwxyz";
  9. string ABCArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  10.  
  11. int encrypt(string text, string argv[]); //encrypt function
  12.  
  13. int main(int argc, string argv[])
  14. {
  15.  
  16. if (argc==2) //check amount of command line arguments
  17. {
  18. if (strlen(argv[1])==26) // check amount of characters in the key
  19. {
  20. for(int i=0; i<26; i++)
  21. {
  22. if((argv[1][i]>=65 && argv[1][i]<=97) || (argv[1][i]>=97 && argv[1][i]<=122)) //check type of character in key
  23. {
  24. for(int p=i+1; p<26; p++) //check repeating letters in key
  25. {
  26. if ((argv[1][i] != argv[1][p]) && ((argv[1][i]+32) != argv[1][p]) && ((argv[1][i]-32) != argv[1][p]))
  27. {
  28.  
  29. }
  30. else
  31. {
  32. printf("The key must not contain the same letter twice\n");
  33. return 1;
  34. }
  35. }
  36. }
  37. else
  38. {
  39. printf("Key must only contain alphabetic characters\n");
  40. return 1;
  41. }
  42. }
  43. }
  44. else
  45. {
  46. printf("Key must contain 26 characters\n");
  47. return 1;
  48. }
  49. }
  50. else
  51. {
  52. printf("Usage: ./substitution key\n");
  53. return 1;
  54. }
  55. string t = get_string("plaintext: ");
  56. printf("ciphertext: %c", encrypt(t, argv));
  57. printf("\n");
  58. return 0;
  59. }
  60.  
  61.  
  62. //encrypt function
  63. int encrypt(string text, string argv[])
  64. {
  65. for(int x = 0; x <= strlen(text); x++)
  66. {
  67. if ((text[x]<65) || (text[x]>90 && text[x]<97) || (text[x]>122))
  68. {
  69. printf("%c", text[x]);
  70. }
  71. else if ((text[x]>=65) && (text[x]<=90))
  72. {
  73. for(int y=0; y<=strlen(ABCArray); y++)
  74. {
  75. if(text[x]==ABCArray[y])
  76. {
  77. printf("%c", toupper(argv[1][y]));
  78. }
  79. }
  80. }
  81. else
  82. {
  83. for(int z=0; z<=strlen(abcArray); z++)
  84. {
  85. if(text[x]==abcArray[z])
  86. {
  87. printf("%c", tolower(argv[1][z]));
  88. }
  89. }
  90. }
  91. }
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement