找出字符串中第一个只出现一次的字符
原型:
bool FindChar(char* pInputString, char* pChar);
输入参数:
char* pInputString:字符串
输出参数(指针指向的内存区域保证有效):
char* pChar:第一个只出现一次的字符
如果无此字符 请输出'.'
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
char getUniqueCharacter(char*s)
{ char order[1000]={0};
int counter[1000]={0};
int i=0;
char temp='a';
char*p=s;
while(*p)
{
temp=*p;//p指向的字符赋给temp
if(counter[temp]==0)//如果temp没出现过,把第i个次序位置留给temp
{
order[i++]=temp; }
counter[temp]++;//temp出现的次数加1
p++;//继续往下搜索
}
//遍历字符串来查账count=1的字符
int j;
for(j=0;j<1000;j++)
if(order[j])//如果次序j的值不为0
{
if(counter[order[j]]==1)//第j个值出现了一次
{
temp=order[j];
break; //把这个值赋给temp
}
else if(counter[order[j]]>1)
{ temp='.';}
}
return temp;
}
int main()
{
char s[1000];
cin.getline(s,1000);
char result= getUniqueCharacter(s);
cout<<result<<endl;
system("pause");
return 0;
}