Guest User

Untitled

a guest
May 5th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. int validate (char* key);
  8. char* ciphrate (char* text, char* key);
  9.  
  10. int main (int argc, string argv[])
  11. {
  12. int valid = validate(argv[1]); //call function to validate the key is 26 char long, no char repeated and no numebers on it
  13. if (valid != 0)
  14. { //prints an error code
  15. if(valid == 1)
  16. {
  17. printf("Usage ./usability KEY\n");
  18. }
  19. else if (valid == 2)
  20. {
  21. printf("The key must contain 26 alphabetic characters\n");
  22. }
  23. else if (valid == 3)
  24. {
  25. printf("The key must contain no repeated characters\n");
  26. }
  27. else if (valid == 4)
  28. {
  29. printf("The key must not contain digits\n");
  30. }
  31.  
  32. return 1; //return a code error
  33. }
  34.  
  35. char* plaintxt = get_string("Plaintext: "); //ask for text to encipher
  36.  
  37. printf("\n");
  38. char* ciphertxt = ciphrate(plaintxt, argv[1]); //call function to encipher the text
  39.  
  40. printf("Ciphertext: %s\n", ciphertxt); //prints the result text
  41.  
  42. return 0;
  43. }
  44.  
  45. int validate (char* key)
  46. {
  47. int lenght = strlen(key); //checks for key lenght
  48. int chars = 0, a = 0, b = 0, c = 0; //some variables
  49.  
  50. if (lenght != 26) //checks if lenght isn't equal to 26
  51. {
  52. return 2; //second error code output
  53. }
  54. for(int k = 0; k < 26; k++)
  55. {
  56. a = isdigit(key[k]); //check every char for a digit and add a value to a
  57.  
  58. }
  59. if (a != 0) //if there's a digit
  60. {
  61. return 4; //fourth error code output
  62. }
  63.  
  64. for(int i = 0; i < 26; i++)
  65. {
  66. c = islower(key[i]);
  67. if (c != 0)
  68. {
  69. key[i] = toupper(key[i]); //transform all chars to uppercase
  70. }
  71. b = b + key[i];
  72. }
  73. if (b != 2015) //the sum of all A - Z chars is 2015
  74. {
  75. return 3; //third error code output
  76. }
  77.  
  78. return 0; //no error code output
  79. }
  80.  
  81.  
  82. char* ciphrate (char* text, char* key)
  83. {
  84. int text_lenght = strlen(text);
  85. char* ciphrated_text = NULL;
  86.  
  87. for(int i = 0; i < text_lenght; i++)
  88. {
  89. int d = 0, e = 0,f = 0;
  90. d = isupper(text[i]); //checks if cahr is uppercase or lower
  91. e = islower(text[i]);
  92. if(d == 0)
  93. {
  94. f = text[i] - 65; //if it's uppercase then substract 65 (for knowing in witch place of the alphabet it's located)
  95. ciphrated_text[i] = key[f]; //and then assign the letter in the key that is in that possition
  96. }
  97. else if(e == 0)
  98. {
  99. f = text[i] - 97; //if it's a lowercase then substract 97 (same reason as in uppercase)
  100. ciphrated_text[i] = key[f] + 32; //assign the letter in the key and add 32 so it's still a lowercase
  101. }
  102. }
  103.  
  104. return ciphrated_text; //return the ciphrated text
  105. }
Add Comment
Please, Sign In to add comment