题目:在字符串中找出第一个只出现一次的字符,如输入“abaccdeff”则输出‘b’
思路:采用一个数组记录出现的次数然后再遍历该数组:
// Find.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
/*
字符char是一个长度为8的数据类型,因此总共有256种可能,于是我们创建一个长度为256的数组
每个字母根据其ASCII码值作为数组的下标对应数组的一个数字,而数组中存储的是每个字符出现的次数
*/
char FindFirstChar(char* pString)
{
if(pString == NULL)
return '\0';
const int N = 256;
int b[N];
for(int i = 0; i < N; ++i)
{
b[i] = 0;
}
char* p = pString;
while (*p != '\0')
{
b[*(p++)]++;
}
p = pString;
while (*p != '\0')
{
if (b[*p] == 1)
{
return *p;
}
++p;
}
return '\0';
}
int _tmain(int argc, _TCHAR* argv[])
{
char* pString = "abcddeeff";
char c = FindFirstChar(pString);
cout<<c<<endl;
getchar();
return 0;
}