Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. int main(int argc, string argv[]) {
  7.  
  8. if (argc != 2) {
  9. printf("Invalid input\n");
  10. return 1;
  11. }
  12.  
  13. int j = atoi(argv[1]);
  14. // j is the input to move the letters however many spaces
  15.  
  16. //prevents the user from using negative integers
  17. while (j < 0) {
  18. printf("Invalid input %i, please supply a positive integer\n", j);
  19. j = get_int();
  20. }
  21.  
  22. if (j > 26) {
  23. j %= 26;
  24. }
  25.  
  26. printf("plaintext: ");
  27. string s = get_string();
  28. //input from user of string to encrypt
  29.  
  30. for (int i = 0; i < strlen(s); i++) {
  31.  
  32. //These if statements is for the uppercase case
  33. if (s[i] <= 'Z' && s[i] >= 'A') {
  34.  
  35. s[i] += j;
  36.  
  37. //wraps back around to uppercase letters
  38. if (s[i] > 'Z') {
  39.  
  40. s[i] -= 26;
  41.  
  42. }
  43. }
  44.  
  45. //these if statements are for the lowercase case
  46. else if (s[i] <= 'z' && s[i] >= 'a') {
  47.  
  48. s[i] += j;
  49.  
  50. // wraps back around to lowercase letters
  51. if (s[i] > 'z') {
  52.  
  53. s[i] -= 26;
  54.  
  55. }
  56. }
  57.  
  58. }
  59. printf ("ciphertext: %s\n", s);
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement