Skip to content

Instantly share code, notes, and snippets.

@CAFxX
Created February 23, 2012 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CAFxX/1893226 to your computer and use it in GitHub Desktop.
Save CAFxX/1893226 to your computer and use it in GitHub Desktop.
Find the first non-repeated character in a string
#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