puzzles/is-isomorphic/is_isomorphic.c
Zoé Cassiopée Gauthier 7e4f02547b Initial commit
2024-04-02 17:01:09 -04:00

44 lines
1.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
char *char_map = calloc(256, sizeof(char));
char *back_map = calloc(256, sizeof(char));
if (argc != 3) {
fprintf(stderr, "%s: Must provide two arguments.\n", argv[0]);
exit(EXIT_FAILURE);
}
char *s = argv[1];
char *t = argv[2];
while (*s && *t) {
if (char_map[(size_t)*s] && char_map[(size_t)*s] != *t) {
fprintf(stderr,
"%s: Character `%c` cannot be remapped from `%c` to `%c`.\n",
argv[0], *s, char_map[(size_t)*s], *t);
exit(EXIT_FAILURE);
}
if (back_map[(size_t)*t] && back_map[(size_t)*t] != *s) {
fprintf(stderr,
"%s: Mapping to `%c` cannot be assigned to both `%c` and `%c`.\n",
argv[0], *t, back_map[(size_t)*t], *s);
exit(EXIT_FAILURE);
}
char_map[(size_t)*s] = *t;
back_map[(size_t)*t] = *s;
s++;
t++;
}
if (*s || *t) {
fprintf(stderr, "%s: Arguments do not have the same length.\n", argv[0]);
exit(EXIT_FAILURE);
}
return 0;
}