Sunday, October 30, 2011

First nonrepeated character in a string

//First nonrepeated character in a string
char first_unique_char_in_string(const string a)
{
  //suppose string is composed of alphabets only ( a~z).
    unsigned char count[26];
    for(int i=0; i<26; i++) count[i] = 0; //initialize
    for(int i=0; i        count[a[i]-'a']++;
    for(int i=0; i        if(count[a[i]-'a']==1)
            return a[i];

    return -1; //no unique char.

}

No comments: