C++ 在无序字符串中查找所有重复的字符
Example:给定字符串“ABCDBGAC”,打印“A B C”
#include <iostream>
#include <string>
using namespace std;
struct ASCII
{
char a;
size_t count;
};
void PrintIterateChar(const string a)
{
string s = a;
for (int i = 0; i < s.size() - 1; i++)
{
if (s[i] == '#') //判断i指针的指向是否为输出过的字符
continue;
int m = 1; //判断j指针的指向是否为输出过的字符
for (int j = i + 1; j <= s.size() - 1; j++)
{
if (s[i] == s[j])
{
if (m == 1)
cout << s[i] << " ";
s[j] = '#'; //对输出过的字符做标记
m = 0; //对输出过的字符做标记
}
}
}
}
void PrintIterateChar2(const string a)
{
ASCII b[128];
for (int i = 0; i <= 128; i++)
{
b[i].a = 0; //ASCII 0 = NULL
b[i].count = 0;
}
for (int i = 0; a[i] != '\0'; i++)
{
b[a[i]].a = a[i];
b[a[i]].count++;
}
for (int i = 0; i < 128; i++)
{
if (b[i].count > 1)
cout << b[i].a << " ";
}
cout << endl;
}
int main()
{
string a;
cin >> a;
PrintIterateChar(a); //请评论你喜欢哪一个?
PrintIterateChar2(a); //请评论你喜欢哪一个?
cout << endl
<< a;
return 0;
}
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有