题目描述
从键盘输入一个字符串,输出第一个只出现了一次的字符,
例如:输入- agdherdgaui 输出- h
输入- xnbmzbc 输出- x
解题思路
开辟一段空间,将输入的字符依次存入空间,若将存字符与之前存入的字符相同则直接在之前字符位置进行计数。当字符串全部遍历一遍之后,输出第一个计数器位置为1的字符即可。
完整代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;
int FirstMeet(string s)
{
int countchar[256] = { 0 };
for (int i = 0; i < s.size(); i++)
{
countchar[s[i]]++;
}
for (int i = 0; i < s.size(); i++)
{
if (countchar[s[i]] == 1)
return i;
}
return -1;
}
int main()
{
string z ;
while (cin >> z)
{
int a = FirstMeet(z);
if (a >= 0)
cout << z[a] << endl;
else
cout << a << endl;
}
system("pause");
return 0;
}