grimoire/c/unique.c

43 lines
804 B
C

// not uniq(1)
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
const char *unique(const char *str, const char **dup) {
const char *alphabet[26] = { NULL };
for(; *str; str += 1) {
uint8_t pos = tolower(*str) - 'a';
if(pos >= 0 && pos <= 25) {
if(alphabet[pos]) {
*dup = str;
return alphabet[pos];
} else {
alphabet[pos] = str;
}
}
}
return NULL;
}
int main(int argc, char **argv) {
const char *str = "";
if(argc > 1) {
str = argv[1];
}
const char *dup = NULL;
const char *idx = unique(str, &dup);
if(dup) {
uint8_t idx_spacing = (int) (idx - str) + 1;
uint8_t dup_spacing = (int) (dup - str) - idx_spacing + 1;
printf("%s\n%*s%*s\n", str, idx_spacing, "^", dup_spacing, "^");
}
return 0;
}