Advertisement
tdttvd

Caesar

Feb 16th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define MAX 100
  7.  
  8. void encode(char *s, int length, int k)
  9. {
  10.     int n;
  11.     for (int i = 0; i < length; ++i)
  12.     {
  13.         if (!isalpha(s[i])) continue;
  14.         s[i] = toupper(s[i]);
  15.         n = s[i];
  16.         n += k;
  17.         while (n > 'Z')
  18.             n -= 26;
  19.         s[i] = (char)n;
  20.     }
  21. }
  22.  
  23. void decode(char *s, int length, int k)
  24. {
  25.     int n;
  26.     for (int i = 0; i < length; ++i)
  27.     {
  28.         if (!isalpha(s[i])) continue;
  29.         s[i] = toupper(s[i]);
  30.         n = s[i];
  31.         n -= k;
  32.         while (n < 'A')
  33.             n += 26;
  34.         s[i] = (char)n;
  35.     }
  36. }
  37.  
  38. int main()
  39. {
  40.     FILE *infile = fopen("Cau3.INP", "r");
  41.     FILE *outfile = fopen("Cau3.OUT", "w");
  42.  
  43.     int n;
  44.     fscanf(infile, "%d", &n);
  45.  
  46.     // Clear input buffer
  47.     while (fgetc(infile) != '\n');
  48.  
  49.     char s1[MAX];
  50.     char s2[MAX];
  51.  
  52.     fgets(s1, MAX, infile);
  53.     fgets(s2, MAX, infile);
  54.  
  55.     int l1 = strlen(s1);
  56.     int l2 = strlen(s2);
  57.  
  58.     encode(s1, l1, n);
  59.     decode(s2, l2, n);
  60.  
  61.     fprintf(outfile, "%s", s1);
  62.     fprintf(outfile, "%s", s2);
  63.  
  64.     fclose(infile);
  65.     fclose(outfile);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement