在C ++中查找两个字符串的不常见字符
在本教程中,我们将讨论一个程序来查找两个字符串的不常见字符。
为此,我们将提供两个字符串。我们的任务是按排序顺序打印出两个字符串中不常见的字符。
示例
#include <bits/stdc++.h>
using namespace std;
const int LIMIT_CHAR = 26;
//查找不常见的字符
void calculateUncommonCharacters(string str1, string str2) {
int isthere[LIMIT_CHAR];
for (int i=0; i<LIMIT_CHAR; i++)
isthere[i] = 0;
int l1 = str1.size();
int l2 = str2.size();
for (int i=0; i<l1; i++)
isthere[str1[i] - 'a'] = 1;
for (int i=0; i<l2; i++) {
if (isthere[str2[i] - 'a'] == 1 || isthere[str2[i] - 'a'] == -1)
isthere[str2[i] - 'a'] = -1;
else
isthere[str2[i] - 'a'] = 2;
}
for (int i=0; i<LIMIT_CHAR; i++)
if (isthere[i] == 1 || isthere[i] == 2 )
cout << (char(i + 'a')) << " ";
}
int main() {
string str1 = "tutorials";
string str2 = "point";
calculateUncommonCharacters(str1, str2);
return 0;
}输出结果
a l n p r s u
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短