Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. // Code techniques not taught in CS50 are from previous programming experience
  7.  
  8. int main(int argc, string argv[])
  9. {
  10. // If the user passes the correct amount of arguments...
  11. if (argc == 2)
  12. {
  13. // Convert the user's argument into an int named argPassed
  14. int argPassed = atoi(argv[1]);
  15. // ...receive input for string plainText
  16. printf("plaintext: ");
  17. string plainText = get_string();
  18.  
  19. printf("ciphertext: ");
  20.  
  21. // Constrain the key to 26 value
  22. argPassed = argPassed % 26;
  23.  
  24. // ...for each character in the string typedName...
  25. for (int i = 0; i < strlen(plainText); i++)
  26. {
  27. // ...create char variable "plainChar" with that chararcter
  28. char plainChar = plainText[i];
  29.  
  30. // ...if the character is not a letter...
  31. if (plainChar > 'z' || plainChar < 'A')
  32. {
  33. // ...print that character
  34. printf("%c", plainChar);
  35. }
  36. // ...if character + argument <= z/Z and >= a/A...
  37. else if ((plainChar + argPassed <= 'z' && plainChar + argPassed >= 'a') || (plainChar + argPassed <= 'Z' && plainChar + argPassed >= 'A'))
  38. {
  39. // ...print the user's input + argument
  40. printf("%c", plainChar + argPassed);
  41. }
  42. // ...if character + argument > z or > Z but < a
  43. else if ((plainChar + argPassed > 'z') || (plainChar + argPassed > 'Z' && plainChar + argPassed < 'a'))
  44. {
  45. printf("%c", plainChar + argPassed - 26);
  46. }
  47. }
  48. // End on a new line
  49. printf("\n");
  50. }
  51. else
  52. {
  53. // Print an error and return 1 if the user enters an incorrect amount of arguments
  54. printf("Please enter an argument in the form of a single integer.\n");
  55. return(1);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement