Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int check(int d, string f[]);
  7. char typeandcase(char pi, int size, char k[]);
  8.  
  9. int main(int argc, string argv[])
  10. {
  11.  
  12. if (check(argc, argv) == 3)
  13. {
  14. string P = get_string("paintext: ");
  15. int z = strlen(P);
  16. string NUM = argv[1]; char m[z + 1];
  17. m[z] = '\n';
  18.  
  19. for (int j = 0; j < z; j ++)
  20. {
  21.  
  22. typeandcase(P[j], z, NUM);
  23. m[j] = typeandcase(P[j], z, NUM);
  24. }
  25.  
  26. printf("ciphertext: %s\n", m);
  27. return 0;
  28.  
  29.  
  30. }
  31. else
  32. {
  33. return 1;
  34. }
  35. }
  36.  
  37. int check(int d, string f[])
  38. {
  39. if (d == 2)
  40. {
  41. for (int i = 0; i < strlen(f[1]); i ++)
  42. {
  43. if (isdigit(f[1][i]))
  44. {
  45. printf("Usage: ./vigenere keyword \n");
  46. return 1 ;
  47. }
  48. }
  49. return 3 ;
  50. }
  51. else
  52. {
  53. printf("Usage: ./vigenere keyword \n");
  54. return 1;
  55. }
  56. return 1;
  57. }
  58.  
  59.  
  60. char typeandcase(char pi, int size, char k[])
  61. {
  62. if (isalpha(pi))
  63. {
  64. for (int i = 0 ; i < size; i++)
  65. {
  66. if (islower(pi) && islower(k[i]))
  67. {
  68. pi -= 'a';
  69. k[i] -= 'a' ;
  70. pi = (pi + k[i]) % 26;
  71. pi += 'a';
  72.  
  73. }
  74. else if (isupper(pi) && isupper(k[i]))
  75. {
  76. pi -= 'A';
  77. k[i] -= 'A' ;
  78. pi = (pi + k[i]) % 26;
  79. pi += 'A';
  80. }
  81.  
  82. }
  83. printf("value: %c\n", pi);
  84.  
  85. }
  86.  
  87.  
  88. return pi;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement