Guest User

Untitled

a guest
Feb 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <cs50.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. //this function checks if the command line argument has any non-alphabetic characters
  8. int valid_command(string cmd);
  9. //this function converts the key to an array of ints that correspond to each character in the key (ex: A or a = 0, B or b = 1, etc.) and returns a pointer to that array
  10. int* convert_key (string cmd);
  11. //calculate the length of the string
  12. int length (string cmd);
  13.  
  14. int main (int argc, string argv[]){
  15. //cmd is used as a bool for the following if-statement- It's used to check if the command line argument (key) has any non-alphabetic characters
  16. int cmd = valid_command(argv[1]);
  17.  
  18. //error checking for if the user input more than 2 command line arguments or if the key has any non-alphabetic characters
  19. if ((argc != 2) || cmd){
  20. printf("Error: Not valid input!n");
  21. return 1;
  22. }
  23. else{
  24. printf("plaintext: ");
  25. string plaintext = get_string();
  26. int size = length(argv[1]); //save the length of the key
  27.  
  28. //convert key to an array of integers
  29. int* delta = convert_key(argv[1]);
  30.  
  31. int* cipher = (int*)malloc((size+1)*sizeof(int));
  32. int* cipher_ptr = cipher;
  33. char* p_ptr = plaintext;
  34.  
  35. //go through each character in plaintext, determine if it's an alphabetic character and whether it's in the A-Z range or a-z range and use a calculation based on that to figure out how the character should be encrypted. This also takes into account wrapping of the key array (if the key array is smaller than plaintext, then once the last character in key is used to encrypt a character in plaintext, the next character should start over from the beginning of key) and also takes into account maintaining the case of the character of plaintext
  36. if (plaintext != NULL){
  37. int i = 0;
  38. while ((*p_ptr) != ''){
  39. //implement wrapping of key array
  40. if (i == size){
  41. i = 0;
  42. }
  43. if (isalpha(*p_ptr)){
  44. //if element is in A-Z range:
  45. if (isupper(*p_ptr)){
  46. *cipher_ptr = ((((*p_ptr)-65)+(delta[i]))%26) + 65;
  47. p_ptr++;
  48. cipher_ptr++;
  49. i++;
  50. }
  51. else {
  52. *cipher_ptr = ((((*p_ptr)-97)+(delta[i]))%26) + 97;
  53. p_ptr++;
  54. cipher_ptr++;
  55. i++;
  56. }
  57. }
  58. else {
  59. //if the character in plaintext is non-alphabetic, just copy that into the cipher array, but don't go on to the next element in key array
  60. *cipher_ptr = *p_ptr;
  61. p_ptr++;
  62. cipher_ptr++;
  63. }
  64. }
  65. }
  66. else {
  67. return 1;
  68. }
  69.  
  70.  
  71.  
  72. //print out the encrypted ciphertext
  73. printf("ciphertext: ");
  74.  
  75. //put into chars
  76. char ciphertext [length(plaintext)];
  77.  
  78. for (int i = 0; i < length(plaintext); i++){
  79. ciphertext[i] = *cipher;
  80. cipher++;
  81. printf("%c",ciphertext[i]);
  82. }
  83. printf("n");
  84.  
  85. return 0;
  86. }
  87.  
  88. }
  89.  
  90. //pass in the key string and return a pointer to array of integers that are integer representations of each character in key string
  91. int* convert_key (string cmd){
  92. char* ptr = cmd;
  93. int* array = (int*) malloc(strlen(cmd)*sizeof(int));
  94. int* array_ptr = array;
  95. while ((*ptr) != ''){
  96. *array_ptr = (toupper((*ptr))-65);
  97. ptr++;
  98. array_ptr++;
  99. }
  100. return array;
  101. }
  102.  
  103. //this function checks if the command line argument has any non-alphabetic character
  104. //return 0 if there are no non-alphabetic characters
  105. //return 1 if there are non-alphabetic characters
  106. int valid_command(string cmd){
  107. char* ptr = cmd;
  108. if (ptr != NULL){
  109. while ((*ptr) != ''){
  110. int value = (toupper((*ptr))-65);
  111. if ((value > 25) || (value < 0)){
  112. return 1;
  113. }
  114. else {
  115. ptr++;
  116. }
  117. }
  118. return 0;
  119. }
  120. else{
  121. return 1; //return 1 if no command was given
  122. }
  123. }
  124.  
  125. int length (string cmd){
  126. int length = 0;
  127. char* ptr = cmd;
  128. while ((*ptr) != ''){
  129. length++;
  130. ptr++;
  131. }
  132. return length;
  133. }
Add Comment
Please, Sign In to add comment