Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- #define LETTERS_IN_ALPHABET (26)
- char encode_character(char character_to_encode, int character_position)
- {
- char temp = character_to_encode - 'A' + character_position;
- temp %= LETTERS_IN_ALPHABET;
- return (temp + LETTERS_IN_ALPHABET) % LETTERS_IN_ALPHABET + 'A';
- }
- char decode_character(char character_to_decode, int character_position)
- {
- char temp = character_to_decode - 'A' - character_position;
- temp %= LETTERS_IN_ALPHABET;
- return (temp + LETTERS_IN_ALPHABET) % LETTERS_IN_ALPHABET + 'A';
- }
- int main(int argc, char *argv[])
- {
- if (argc == 1) {
- printf("Please provide text to encode or decode.\n");
- return 0;
- }
- char (*function_to_use)(char, int);
- int argstart = 1;
- argv[1][1] = toupper(argv[1][1]);
- if (strcmp("-E", argv[1]) == 0) {
- function_to_use = encode_character;
- argstart++;
- } else if (strcmp("-D", argv[1]) == 0) {
- function_to_use = decode_character;
- argstart++;
- } else {
- function_to_use = encode_character;
- }
- for (int i = argstart, j = 0; i < argc; i++) {
- char *string = argv[i];
- while (*string) {
- if (isalpha(*string)) {
- printf("%c", function_to_use(toupper(*string), j));
- j++;
- } else if (*string == ' ') {
- printf(" ");
- }
- *string++;
- }
- printf(" ");
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement