Determine if a string has any duplicate characters
This commit is contained in:
parent
7f1dffc5e5
commit
b137d99a96
1 changed files with 43 additions and 0 deletions
43
c/unique.c
Normal file
43
c/unique.c
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
// 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;
|
||||||
|
}
|
Loading…
Reference in a new issue