Jun 9, 2009

Given a string with multiple occurrences of characters (e.g. abaabcddeadegfhhh) return a string that contains characters that appear an odd number of

Given a string with multiple occurrences of characters (e.g. abaabcddeadegfhhh) return a string that contains characters that appear an odd number of times. (In this case it would be (dgfh)).

char* foo(char* str)
{
if(!str) return NULL;
char t[256];
int len=strlen(str);
for(int i=0;i<256;i++)
t[i]=0;
for(int i=0;i < len;i++)
t[str[i]]++;
int h1=h2=0;
while(h2 < len)
{
if(t[str[h2]]%2)
str[p1++]=str[p2];
p2++;
}
str[p1]='\0';
return str;
}

No comments:

Post a Comment