Advertisement
kabman7

vigenere.c

Dec 5th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 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 shift(char c);
  8.  
  9. int main(int argc, string argv[])
  10. {
  11. string key = argv[1];
  12. if (argc== 2)
  13. {
  14.  
  15. for(int i=0; i<strlen(key); i++)
  16. {
  17. if(isalpha(key[i]))
  18. {
  19. continue;
  20. }
  21. else
  22. {
  23. printf("Usage: ./vigenere keyboard\n");
  24. return 1;
  25. }
  26. }
  27.  
  28. }
  29. else
  30. {
  31. printf("Usage: ./vigenere keyboard\n");
  32. return 1;
  33. }
  34.  
  35. string text=get_string("plaintext: ");
  36. printf("ciphertext: ");
  37. int ktracker=strlen(key);
  38. int counter=0;
  39. for(int i=0;i<strlen(text);i++)
  40. {
  41. if(text[i]== '\40')
  42. {
  43. printf("%c",text[i]);
  44. }
  45. else
  46. {
  47. printf("%c",(text[i]+shift(key[counter%ktracker])));
  48. counter++;
  49. }
  50.  
  51. }
  52. printf("\n");
  53. }
  54.  
  55. int shift(char c)
  56. {
  57. int ans=0;
  58. if(isupper(c))
  59. {
  60. ans=((c-65) % 26);
  61. }
  62.  
  63. else if(islower(c))
  64. {
  65.  
  66. ans=((c-97) % 26);
  67. }
  68. return ans;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement