本文共 472 字,大约阅读时间需要 1 分钟。
题目:在一个字符串中找到第一个只出现一次的字符,如输入abaccdeff,则输出b;具体实现如下:
- #include <iostream>
- #include <string>
- using namespace std;
- void FindChar(const string &strBuf)
- {
- int nArr[256];
- int i;
- for (i = 0; i < 256; i++)
- {
- nArr[i] = 0;
- }
- for (i = 0; i < strBuf.size(); i++)
- {
- nArr[strBuf[i]]++;
- }
- for (i = 0; i < 256; i++)
- {
- if (nArr[i] == 1)
- {
- cout<<(char)i<<endl;
- return;
- }
- }
- }
- int main()
- {
- string str = "dfdsa4qwerfsdasdf4vwers";
- FindChar(str);
- system("pause");
- return 0;
- }
转载地址:http://pmoii.baihongyu.com/