题目:在字符串中找出第一个只出现一次的字符。如输入“abaccdeff”,则输出’b‘。
char FirstNotRepeatingChar(char* pString);
bool InvalidInput = false;
char FirstNotRepeatingChar(char* pString)
{
if (pString == NULL)
{
InvalidInput = true;
return 0;
}
unsigned int HashTable[256];
int i=0;
while (i < 256)
{
HashTable[i++] = 0;
}
char *pIndexStr = pString;
while ( *pIndexStr != '\0')
{
HashTable[*(pIndexStr++)]++;
}
pIndexStr = pString;
while (*pIndexStr != '\0')
{
if (HashTable[*pIndexStr] == 1)
break;
pIndexStr++;
}
return *pIndexStr;
}
void test()
{
char TestStr[] = "aacddfhji";
printf("FirstNotRepeatingChar Result = %c\n",FirstNotRepeatingChar(TestStr));
}
int _tmain(int argc, _TCHAR* argv[])
{
test();
return 0;
}