尋找字符串中第一個只出現一次打字符
代碼如下:
#include <iostream>
#include <string>
using namespace std;
char findfirstoncechar(string &str)
{
int arr[255] = {0};
int i;
for (i = 0; i<str.size();i++)
{
arr[str[i]]++;
}
for(i = 0; i < 255 ; i++ )
{
if(arr[i] == 1)
{
return i;
}
}
return -1;
}
int main ()
{
string str="abcecbag";
char c= findfirstoncechar(str);
cout<< c << endl;
return 0;
}