Guest User

Untitled

a guest
May 30th, 2016
122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * pset 2
  3. * Vigenere's Cipher
  4. *
  5. * Mike
  6. *
  7. * Scrambles a message by k characters
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <cs50.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. int main(int argc, string argv[])
  17.  
  18. // get valid input
  19.  
  20. {
  21. while(argc != 2)
  22. {
  23. printf("error\n");
  24. return 1;
  25. }
  26.  
  27. // convert input into key
  28.  
  29. string scramble = GetString();
  30. int i;
  31. for (i = 0; i < strlen(scramble); i++)
  32. if isalpha(scramble[i])
  33. {
  34. int k = toupper(argv[1][i]);
  35. int key = (k - 64);
  36.  
  37. // print character up/low
  38.  
  39. if(isupper(scramble[i]))
  40. {
  41. int code = (scramble[i] - 65 + key) %26;
  42. printf("%c", (char) code + 65);
  43. }
  44.  
  45. //lowercase
  46.  
  47. else if(islower(scramble[i]))
  48. {
  49. int code = (scramble[i] - 97 + key) %26;
  50. printf("%c", (char) code + 97);
  51. }
  52. else (printf("%c", scramble[i]));
  53. }
  54. else (printf("%c", scramble[i]));
  55. printf("\n");
  56. }
RAW Paste Data