判断一个个输出的字符串中的字符是不是唯一的
比如hello中有两个l就不是唯一的
world没有相同的字符,就是唯一的
源代码如下:
#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;
bool unique(string &);
int main()
{
string str = "Hello World!";
bool flag = unique(str);
if (flag)
cout<<"没有相同的字符!"<<endl;
system("pause");
return 0;
}
bool unique(string& str)
{
bool a[256];
memset(a,0,sizeof(a));
int size = str.length();
for(int i = 0; i < size; i++)
{
int v = str[i];
if(a[v])
{
cout<<"有相同的字符,且为:"<<str[i]<<endl;
return false;
}
a[v] = true;
}
return true;
}