SHARE
TWEET

encoding

a guest Mar 23rd, 2015 25 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2.  * License, v. 2.0. If a copy of the MPL was not distributed with this
  3.  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4.  
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8.  
  9. #define LETTERS_IN_ALPHABET (26)
  10.  
  11. char encode_character(char character_to_encode, int character_position)
  12. {
  13.         char temp = character_to_encode - 'A' + character_position;
  14.         temp %= LETTERS_IN_ALPHABET;
  15.         return (temp + LETTERS_IN_ALPHABET) % LETTERS_IN_ALPHABET + 'A';
  16. }
  17.  
  18. char decode_character(char character_to_decode, int character_position)
  19. {
  20.         char temp = character_to_decode - 'A' - character_position;
  21.         temp %= LETTERS_IN_ALPHABET;
  22.         return (temp + LETTERS_IN_ALPHABET) % LETTERS_IN_ALPHABET + 'A';
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.         if (argc == 1) {
  28.                 printf("Please provide text to encode or decode.\n");
  29.                 return 0;
  30.         }
  31.  
  32.         char (*function_to_use)(char, int);
  33.         int argstart = 1;
  34.        
  35.         argv[1][1] = toupper(argv[1][1]);
  36.         if (strcmp("-E", argv[1]) == 0) {
  37.                 function_to_use = encode_character;
  38.                 argstart++;
  39.         } else if (strcmp("-D", argv[1]) == 0) {
  40.                 function_to_use = decode_character;
  41.                 argstart++;
  42.         } else {
  43.                 function_to_use = encode_character;
  44.         }
  45.  
  46.         for (int i = argstart, j = 0; i < argc; i++) {
  47.                 char *string = argv[i];
  48.                 while (*string) {
  49.                         if (isalpha(*string)) {
  50.                                 printf("%c", function_to_use(toupper(*string), j));
  51.                                 j++;
  52.                         } else if (*string == ' ') {
  53.                                 printf(" ");
  54.                         }
  55.                         *string++;
  56.                 }
  57.                 printf(" ");
  58.         }
  59.         printf("\n");
  60.         return 0;
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top