Advertisement
shadowsofme

Word Not Reading

Sep 30th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5.  
  6. char encrypt(char * ch, char * key);
  7. char decrypt(char * ch, char * key);
  8. char generateKey(char* ch);
  9.  
  10. int main(int argc, char * argv[])
  11. {  
  12.     int choice;
  13.     char ch, j, k;
  14.     bool loop = true;
  15.     FILE *fin, *fout;
  16.  
  17.     if (argc != 5)
  18.     {
  19.         printf ("Usage: cipher option key infile, outfile\n");
  20.         printf ("Option 1 for encryption and 2 for decryption\n");
  21.         exit(1);
  22.     }
  23.    
  24.     choice = atoi(argv[1]);
  25.     k = atoi(argv[2]);
  26.    
  27.     fin = fopen(argv[3], "r");
  28.     fout = fopen(argv[4], "w");
  29.    
  30.     if (fin ==  NULL || fout == NULL)
  31.     {
  32.         printf("File could not be opened\n");
  33.         exit(1);
  34.     }
  35.  
  36.     while ( fscanf(fin, "%c", &ch) != EOF )
  37.     {      
  38.         if (choice == 1){
  39.             fprintf(fout, "%c", encrypt(ch, k));
  40.         }else if (choice == 2){
  41.             fprintf(fout, "%c", decrypt(ch, k));
  42.         }else {
  43.             printf ("Usage: cipher option key infile, outfile\n");
  44.             printf ("Option 1 for encryption and 2 for decryption\n");
  45.             exit(1);
  46.         }
  47.     }
  48.  
  49.     fclose(fin);
  50.     fclose(fout);
  51.  
  52.     return 0;
  53. }
  54.  
  55. char encrypt(char * word, char * key)
  56. {
  57.     printf("Entered encrypt method.\n");
  58.     printf("%s\n", *word);
  59.     char * alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  60.     int i, j;
  61.     char result = "";
  62.    
  63.     for (i = 0; i < sizeof(word); i++){
  64.         for (j = 0; j < sizeof(alphabet); j++){
  65.             if (word[i] == alphabet[j]){
  66.                 printf("Checking word against alphabet.\n");
  67.                 strcat(key[j], result);
  68.                 printf("Finished check; appended to result.\n");
  69.                 printf("%s\n", result);
  70.             }else if (word[i] == " "){
  71.                 strcat(" ", result);
  72.             }
  73.         }
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement