forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonChars.cpp
69 lines (60 loc) · 1.42 KB
/
commonChars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<string>
using namespace std;
int commonChars(string line1, string line2, char * charArray);
void sort(char * charArray, int size);
void printArray(char * charArray, int size);
void printArray(char * charArray, int size) {
for (int i = 0; i < size; i++) {
cout << charArray[i];
}
}
int commonChars(string line1, string line2, char * charArray) {
string returnString = "";
bool flag = true;
int counter = 0;
for (int i = 0; i < line1.length(); i++) {
for (int j = 0; j < line2.length(); j++) {
if (line1.at(i) == line2.at(j)) {
for (int k = 0; k < counter; k++) {
if (charArray[k] == line1.at(i))
flag = false;
}
if (flag) {
charArray[counter] = line1.at(i);
counter++;
}
flag = true;
}
}
}
return counter;
}
void sort(char * charArray, int size) {
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (charArray[i] > charArray[j]) {
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
}
}
}
}
int main() {
string text1, text2;
string stringToSort = "";
int largerSize = text1.length();
if (text2.length() > largerSize)
largerSize = text2.length();
char * characters = new char[largerSize];
cin >> text1;
cin >> text2;
int size = 0;
size = commonChars(text1, text2, characters);
sort(characters, size);
printArray(characters, size);
cout << endl;
system("pause");
return 0;
}