Jim

first commit

  1 +CC = gcc
  2 +CFLAGS=-I. -O2 -std=c99 -pedantic -Wall
  3 +%.o: %.c
  4 + $(CC) -c -o $@ $< $(CFLAGS)
  5 +
  6 +main: main.o
  7 + $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
  8 +
No preview for this file type
No preview for this file type
  1 +#include <stdio.h>
  2 +#include <assert.h>
  3 +#include <string.h>
  4 +#include <stdlib.h>
  5 +
  6 +
  7 +
  8 +static const char alphabet[] =
  9 + "abcdefghijklmnopqrstuvwxyz"
  10 + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  11 + "0123456789";
  12 +
  13 +
  14 +char encode ( char* temp, char* from, size_t from_size, const char* alphabet, size_t alphabet_size){
  15 +
  16 + for (size_t i=0; i < from_size; i++){
  17 +
  18 + char found_flag = 0;
  19 + size_t j;
  20 + for (j=0; j < alphabet_size; j++){
  21 +
  22 + if ( from[i] == alphabet[j] ) {
  23 + found_flag = 1;
  24 + break;
  25 + }
  26 + }
  27 + if (!found_flag){
  28 +
  29 + return 0; //inversion because of function logic
  30 + }
  31 +
  32 + temp[i] = j; //encoded alphabet characters
  33 +
  34 + }
  35 +
  36 + return 1;
  37 +
  38 +}
  39 +
  40 +
  41 +
  42 +
  43 +
  44 +void walk (char* temp, size_t temp_size, size_t alphabet_size){
  45 +
  46 + for ( size_t i=0; i < temp_size; i++){
  47 +
  48 + char r = temp[temp_size - 1 - i];
  49 +
  50 + if ( (r+1) != alphabet_size ){
  51 +
  52 + temp [temp_size - 1 - i] ++;
  53 +
  54 + return;
  55 + }
  56 +
  57 + temp[temp_size - 1 - i] = 0;
  58 +
  59 + }
  60 +
  61 +}
  62 +
  63 +
  64 +int generate (char* from, char* to, const char* alphabet){
  65 +
  66 +
  67 +
  68 + assert(from);
  69 + assert(to);
  70 + assert(alphabet);
  71 +
  72 +
  73 +
  74 + size_t from_size = strlen (from);
  75 + size_t alphabet_size = strlen (alphabet);
  76 +
  77 + if (strlen (to) != from_size) {
  78 +
  79 + return 1;
  80 + }
  81 +
  82 + char* temp = malloc (from_size);
  83 +
  84 + if (!encode (temp, from, from_size, alphabet, alphabet_size)){
  85 + free (temp);
  86 + return 2;
  87 + }
  88 +
  89 +
  90 + char* to_encoded = malloc (from_size);
  91 +
  92 + if (!encode (to_encoded, to, from_size, alphabet, alphabet_size)){
  93 + free (temp);
  94 + free (to_encoded);
  95 + return 3; //can't encode "to"
  96 + }
  97 +
  98 +
  99 +
  100 + for (size_t i = 0; i < from_size; i++){
  101 + printf ("%d ", (int)temp[i]);
  102 + }
  103 +
  104 + printf ("\n");
  105 +
  106 + while (1){
  107 +
  108 + walk ( temp, from_size, alphabet_size);
  109 +
  110 + for (size_t i = 0; i < from_size; i++){
  111 + printf ("%d ", (int)temp[i]);
  112 + }
  113 +
  114 + printf ("\n");
  115 +
  116 + }
  117 +
  118 + free (temp);
  119 + free (to_encoded);
  120 + return 0;
  121 +
  122 +}
  123 +
  124 +
  125 +
  126 +
  127 +int main(){
  128 + char from [] = "aaa";
  129 + char to [] = "999";
  130 +
  131 + generate(from, to, alphabet);
  132 +
  133 +
  134 +
  135 + return 0;
  136 +
  137 +}
  138 +
No preview for this file type