Advertisement
cd62131

CaesarCipher

Jul 23rd, 2014
1,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. static int decode(char *line) {
  5.   const char *the = "the";
  6.   int i, key = 0;
  7.   while (!strstr(line, the)) {
  8.     for (i = 0; i < strlen(line); i++) {
  9.       if (isupper(line[i]))
  10.         line[i] = (line[i] - 'A' + 1) % 26 + 'A';
  11.       else if (islower(line[i]))
  12.         line[i] = (line[i] - 'a' + 1) % 26 + 'a';
  13.     }
  14.     key++;
  15.   }
  16.   return key;
  17. }
  18. int main(void) {
  19.   char line[BUFSIZ];
  20.   int key;
  21.   printf("cipher text: ");
  22.   fgets(line, BUFSIZ, stdin);
  23.   key = decode(line);
  24.   printf("key: %d\n", key);
  25.   printf("plain text: %s", line);
  26.   return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement