Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1.  
  2. #include <cs50.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7.  
  8. int main(int argc, char* key[])
  9. {
  10. string k = key[1];
  11. // checking if totoal number of arcument in 2
  12. if (argc != 2 )
  13. {
  14. printf("Usage: ./substitution key");
  15. return 1;
  16. }
  17. // checking if all the given arguments are letters loop is to check every element
  18. for (int i = 0; i <26; i++)
  19. {
  20. if (!isalpha(*(k+i)))
  21. {
  22. printf("Usage: ./substitution key");
  23. return 1;
  24. }
  25. }
  26.  
  27. int len = strlen(k);
  28. //checking if given argument has length of 26
  29. if (len != 26)
  30. {
  31. printf("Key must contain 26 characters.\n");
  32. return 1;
  33. }
  34. // checking ig there are any duplicate keys
  35. for (int i=0; i<25; i++)
  36. {
  37. for (int j=i+1; j<26; j++)
  38. {
  39. if (k[i] == k[j])
  40. {
  41. printf("Key must not contain double character.");
  42. exit (1);
  43. }
  44. }
  45. }
  46. //getting user input
  47. string plaintext = get_string("plaintext: ");
  48. printf("ciphertext: ");
  49. //converting plain text into cypher text
  50. for (int i = 0, j = 0, n = strlen(plaintext); i < n; i++)
  51. {
  52. if (islower(plaintext[i]))
  53. {
  54. printf("%c", tolower(k[plaintext[i] - 97]));
  55. j++;
  56. }
  57. else if (isupper(plaintext[i]))
  58. {
  59. printf("%c", toupper(k[plaintext[i] - 65]));
  60. j++;
  61. }
  62. else
  63. {
  64. printf("%c", plaintext[i]);
  65. j++;
  66. }
  67. }
  68. printf("\n");
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement