在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
/*在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b*/
#include <iostream>
#define N 30
using namespace std;
void search(char *inputstr)
{
char map[26] = {0};
int j = 0;
int len = strlen(inputstr);
for(int i = 0; i < len; i++)
map[inputstr[i] - 'a']++;
for(j = 0; j < len; j++)
if(map[inputstr[j] - 'a'] == 1)
{
cout << "the first char in input string is: " << inputstr[j] << endl;
break;
}
if(j >= len)
cout << "not find." << endl;
}
int main()
{
char inputstr[N];
cout << "please input the string:" << endl;
cin.getline(inputstr, N);
search(inputstr);
system("pause");
return 0;
}