#include <string>
#include <iostream>
using namespace std;
char Findchar(char* pInputString)
{
if (!pInputString) return 0; //输入不合法
int hashTable[256] = { 0 }; //创建一个哈希表(即创建一个数组),并初始化
//确定字符串中每个字符出现的次数
char* pHashKey = pInputString; //pInputString指针的首地址赋给pHashKey,*pHashKey对应字符串的值
while (*pHashKey != '\0')
{
hashTable[*(pHashKey)]++;
pHashKey++;
}
//找到字符串中只出现一次的那个字符
pHashKey = pInputString; //指针再次指向首地址,再遍历一次
while (*pHashKey != '\0')
{
if (hashTable[*pHashKey] == 1)
return *pHashKey;
pHashKey++;
}
return 0; //如果这个字符串为空,或者字符串中的每个字符都至少出现两次
}
int main()
{
char str[1000];
gets(str);
char temp = Findchar(str);
if (temp == 0) cout << "." << endl;
else cout << temp << endl;
return 0;
}
华为OJ题目(一):找出字符串中第一个出现一次的字符
最新推荐文章于 2022-06-29 22:01:54 发布