Sha256: aed9556d35a5e87894ccbda6657540f7869e74145110cce9fc8196a1a0a495fb

Contents?: true

Size: 1.55 KB

Versions: 265

Compression:

Stored size: 1.55 KB

Contents

#include "atbash_cipher.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>

#define GROUP_SIZE      (5)

static void remove_trailing_space(char *string)
{
   if (string[strlen(string) - 1] == ' ') {
      string[strlen(string) - 1] = '\0';
   }
}

static bool is_valid_character(char character)
{
   return isalpha(character) || isdigit(character);
}

static char get_encoded_character(char character)
{
   return (isdigit(character) ? character : 'z' - (character - 'a'));
}

static char get_decoded_character(char character)
{
   return (isdigit(character) ? character : 'a' + ('z' - character));
}

char *atbash_encode(const char *input)
{
   char *output = malloc(strlen(input) + (strlen(input) / GROUP_SIZE) + 1);
   int group_count = 0;

   output[0] = '\0';

   for (size_t i = 0; i < strlen(input); i++) {
      if (is_valid_character(tolower(input[i]))) {
         char encoded_character = get_encoded_character(tolower(input[i]));

         strncat(output, &encoded_character, 1);

         if (++group_count == GROUP_SIZE) {
            strcat(output, " ");
            group_count = 0;
         }
      }
   }

   remove_trailing_space(output);

   return output;
}

char *atbash_decode(const char *input)
{
   char *output = malloc(strlen(input) + (strlen(input) / GROUP_SIZE));

   output[0] = '\0';

   for (size_t i = 0; i < strlen(input); i++) {
      if (is_valid_character(input[i])) {
         char decoded_character = get_decoded_character(input[i]);

         strncat(output, &decoded_character, 1);
      }
   }

   return output;
}

Version data entries

265 entries across 265 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.179 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.178 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.177 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.176 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.175 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.174 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.173 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.172 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.171 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.170 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.169 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.167 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.166 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.165 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.164 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.163 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.162 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.161 tracks/c/exercises/atbash-cipher/src/example.c
trackler-2.2.1.160 tracks/c/exercises/atbash-cipher/src/example.c