我知道如何检查字符串是否具有唯一字符,但我想显示 NOT UNIQUE,即使它们的大小写不同
例如-我的算法
string = "dhAra"=> 唯一的
我认为更好的是它显示 NOT UNIQUE 因为它有两次 'a'
#include<iostream>
int main()
{
string str = "dhAra";
bool arr[128] = {0};
for (unsigned int i = 0; i < str.length() ; i++)
{
int val = str[i]; //i think something needs to change here
cout << val << endl;
if(arr[val])
{
cout << " not unique" ;
return 0;
}
else
{ arr[val] = 1;}
}
cout << "unique" ;
return 0 ;
}
最佳答案
您可以对所有字符使用 toupper
或 tolower
以确保捕获仅大小写不同的重复字符:
int val = toupper(str[i]); // tolower would be fine as well
作为旁注,在 C++ 中将 true
分配给 bool
的规范方法是
arr[val] = true; // rather than arr[val] = 1
虽然这两种方式都很好。
关于c++ - 检查字符串是否具有唯一字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15623573/