Advertisement
shadowsofme

Problem code

Oct 5th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. void initializeEncryptArray(char key[], char encrypt[]);
  8. void initializeDecryptArray(char encrypt[], char decrypt[]);
  9. char * removeDuplicates(char word[]);
  10. int targetFound(char charArray[], int num, char target);
  11. void processInput(FILE *inf, FILE * outF, char substitute[]);
  12.  
  13. int main(int argc, char * argv[])
  14. {  
  15.     int choice;
  16.     char *ch;
  17.     bool loop = true;
  18.     FILE *fin, *fout;
  19.  
  20.     if (argc != 5)
  21.     {
  22.         printf ("Usage: cipher option key infile, outfile\n");
  23.         printf ("Option 1 for encryption and 2 for decryption\n");
  24.         exit(1);
  25.     }
  26.    
  27.     choice = atoi(argv[1]);
  28.     char *k = argv[2];
  29.    
  30.     fin = fopen(argv[3], "r");
  31.     fout = fopen(argv[4], "w");
  32.    
  33.     if (fin ==  NULL || fout == NULL)
  34.     {
  35.         printf("File could not be opened\n");
  36.         exit(1);
  37.     }
  38.    
  39.     char encrypt[26], decrypt[26];
  40.     initializeEncryptArray(k, encrypt);
  41.     initializeDecryptArray(encrypt, decrypt);
  42.  
  43.     if (choice == 1){
  44.         processInput(fin, fout, encrypt);
  45.     }else if (choice == 2){
  46.         processInput(fin, fout, decrypt);
  47.     }else {
  48.         printf ("Usage: cipher option key infile, outfile\n");
  49.         printf ("Option 1 for encryption and 2 for decryption\n");
  50.         exit(1);
  51.     }
  52.    
  53.     fclose(fin);
  54.     fclose(fout);
  55.  
  56.     return 0;
  57. }
  58.  
  59. void initializeEncryptArray(char key[], char encrypt[])
  60. {
  61.     int i = 0, j = 90;
  62.    
  63.     char *endkey = removeDuplicates(key);
  64.     printf("%s\n", endkey);
  65.    
  66.     while(endkey[i] != '\0'){
  67.         encrypt[i] = endkey[i];
  68.         i++;
  69.     }
  70.  
  71.     while(j >= 65){
  72.         if (targetFound(encrypt, i, (char)j) != 0){
  73.             encrypt[i] = (char)j;
  74.             i++;
  75.         }
  76.         j--;
  77.     }
  78.     printf("Finished encryption.\n");
  79.     printf("%c\n", encrypt[0]);
  80. }
  81.  
  82. void initializeDecryptArray(char encrypt[], char decrypt[])
  83. {
  84.     int i = 0, j = 0;
  85.     while (i < 26){
  86.         decrypt[((int)(encrypt[i]))-65] = (char) (i + 65);
  87.         i++;
  88.     }
  89. }
  90.  
  91.  
  92. char * removeDuplicates(char word[]){
  93.     int i, j;
  94.     bool match = false;
  95.    
  96.     char *result = "";
  97.     char end[26] = "";
  98.     for (i = 0; i < (strlen(word)+1); i++){
  99.         if (i < strlen(word)){
  100.             for (j = 0; j <= strlen(end); j++){
  101.                 if (end[j] == word[i]){
  102.                     match = true;
  103.                 }
  104.             }
  105.             if (match != true){
  106.                 end[i] = word[i];
  107.             }
  108.         }else {
  109.             end[i] = '\0';
  110.         }
  111.     }
  112.    
  113.     word = end;
  114.     return word;
  115. }
  116.  
  117. int targetFound(char charArray[], int num, char target){
  118.     int i = 0;
  119.     while (i <= num){
  120.         if (charArray[i] == target){
  121.             return 1;
  122.         }
  123.         i++;
  124.     }
  125.     return 0;
  126. }
  127.  
  128. void processInput(FILE *inF, FILE * outF, char substitute[]){
  129.     int parsed = 0;
  130.     char ch;
  131.    
  132.     while ( fscanf(inF, "%s", &ch) != EOF ){
  133.         if(isupper(ch)){
  134.             parsed = ((char) ch) - 65;
  135.             fprintf(outF, "%c", substitute[parsed]);
  136.         }else if(islower(ch)){
  137.             parsed = ((char) ch) - 97;
  138.             fprintf(outF, "%c", substitute[parsed + 32]);
  139.         }else {
  140.             parsed = ch;
  141.             fprintf(outF, "%c", substitute[parsed]);
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement