Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. int main(int argc , string argv[])
  6. {
  7. if (argc == 2 )
  8. {
  9. int key_start_over = 0 , length_of_key = strlen(argv[1]);
  10. string key = argv[1];
  11.  
  12. for (int i = 0; i < length_of_key ; i ++)
  13. {
  14. while (key[i] >= '0' && key[i]<= '9') /* accept no numbers at the key */
  15. {
  16. printf("Usage: %s k\n",argv[0]);
  17. return 1 ;
  18. }
  19. }
  20. printf("plaintext:");
  21. string c = get_string(); /* the plaintext */
  22. printf("ciphertext:");
  23. if (c!= NULL)
  24. {
  25. for (int i2 = 0 , length_c = strlen(c) ; i2 < length_c ; i2++)
  26. {
  27. if (isupper(key[key_start_over]))
  28. {
  29. key[key_start_over] -= 'A';
  30. /* if the character is capital take 65 from it // ex: if char is 'A' that means character equals 0 */
  31. }
  32. if (islower(key[key_start_over]))
  33. {
  34. key[key_start_over] -= 'a';
  35. /* if the character is small take 97 from it // ex: if char is 'b' that means character equals 1 */
  36. }
  37.  
  38. if (isalpha (c[i2]) && c[i2] != ' ') /* if the character is a letter and its not a "space" */
  39. {
  40. if (isupper(c[i2]) && c[i2] + key[key_start_over] > 'Z' )
  41. { /*if the character is capital and if the character + the key is bigger than Z minus 26 which means start from the beginning */
  42. printf("%c",((c[i2]+key[key_start_over]) - 26) );
  43. }
  44. else if(isupper(c[i2]) && (c[i2] + key[key_start_over] ) <= 'Z' )
  45. {
  46. printf("%c",(c[i2]+key[key_start_over]));
  47. }
  48. if (islower(c[i2]) && c[i2] + key[key_start_over] > 'z')
  49. { /*if the character is small and if the character + the key is bigger than z minus 26 which means start from the beginning */
  50. printf("%c",((c[i2] + key[key_start_over]) - 26));
  51. }
  52. else if (islower(c[i2]) &&( c[i2] + key[key_start_over]) <= 'z')
  53. {
  54. printf("%c",(c[i2] + key[key_start_over]));
  55. }
  56. key_start_over ++ ;
  57. if (key_start_over >= length_of_key)
  58. { /* if the key letter ends start over */
  59. key_start_over = 0 ;
  60. }
  61. }
  62. else
  63. { /* if the character is a space print it */
  64. printf("%c",c[i2]);
  65. }
  66. }
  67. }
  68. printf("\n");
  69. }
  70. else
  71. { // if the command-line-argument is less or bigger than 2 print this error message //
  72. printf("Usage: %s k\n",argv[0]);
  73. return 1 ;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement