Jim

first commit

CC = gcc
CFLAGS=-I. -O2 -std=c99 -pedantic -Wall
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)
main: main.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
... ...
No preview for this file type
No preview for this file type
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
static const char alphabet[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789";
char encode ( char* temp, char* from, size_t from_size, const char* alphabet, size_t alphabet_size){
for (size_t i=0; i < from_size; i++){
char found_flag = 0;
size_t j;
for (j=0; j < alphabet_size; j++){
if ( from[i] == alphabet[j] ) {
found_flag = 1;
break;
}
}
if (!found_flag){
return 0; //inversion because of function logic
}
temp[i] = j; //encoded alphabet characters
}
return 1;
}
void walk (char* temp, size_t temp_size, size_t alphabet_size){
for ( size_t i=0; i < temp_size; i++){
char r = temp[temp_size - 1 - i];
if ( (r+1) != alphabet_size ){
temp [temp_size - 1 - i] ++;
return;
}
temp[temp_size - 1 - i] = 0;
}
}
int generate (char* from, char* to, const char* alphabet){
assert(from);
assert(to);
assert(alphabet);
size_t from_size = strlen (from);
size_t alphabet_size = strlen (alphabet);
if (strlen (to) != from_size) {
return 1;
}
char* temp = malloc (from_size);
if (!encode (temp, from, from_size, alphabet, alphabet_size)){
free (temp);
return 2;
}
char* to_encoded = malloc (from_size);
if (!encode (to_encoded, to, from_size, alphabet, alphabet_size)){
free (temp);
free (to_encoded);
return 3; //can't encode "to"
}
for (size_t i = 0; i < from_size; i++){
printf ("%d ", (int)temp[i]);
}
printf ("\n");
while (1){
walk ( temp, from_size, alphabet_size);
for (size_t i = 0; i < from_size; i++){
printf ("%d ", (int)temp[i]);
}
printf ("\n");
}
free (temp);
free (to_encoded);
return 0;
}
int main(){
char from [] = "aaa";
char to [] = "999";
generate(from, to, alphabet);
return 0;
}
... ...
No preview for this file type