Created
February 23, 2012 15:10
-
-
Save CAFxX/1893226 to your computer and use it in GitHub Desktop.
Find the first non-repeated character in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
unsigned char* get_first_unique_char(unsigned char* str, const int len) { | |
int pos[256] __attribute__ ((__aligned__(64))), i, p; | |
if (str == NULL || len <= 0) | |
return NULL; | |
for (i=0; i<256; i++) | |
pos[i] = -1; | |
for (i=0; i<len; i++) | |
pos[str[i]] = ( pos[str[i]] == -1 ? i : -2 ); | |
for (i=0, p=len; i<256; i++) | |
p = ( pos[i] >= 0 && pos[i] < p ? pos[i] : p ); | |
return p==len ? NULL : str+p; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment