Below is corrected working code. Basically the only correction that was needed to do is to make both loops have upper bound of niz.length()
instead of niz.length() - 1
.
Variant 1:
Try it online!
#include <string>
#include <iostream>
using namespace std;
int main() {
string niz1, niz2, niz3 = "";
cout << "string 1: ";
getline(cin, niz1);
cout << "string 2: ";
getline(cin, niz2);
for (int i = 0; i < niz1.length(); i++) {
for (int j = 0; j < niz2.length(); j++) {
if (niz1[i] == niz2[j])
niz3 += niz1[i];
}
}
cout << "Same letters are: " << niz3 << endl;
return 0;
}
Input:
string 1: adbc
string 2: cde
Output:
Same letters are: dc
Also you may want to sort letters and make them unique, then you need to use std::set
too like in code below:
Variant 2:
Try it online!
#include <string>
#include <iostream>
#include <set>
using namespace std;
int main() {
string niz1, niz2, niz3 = "";
cout << "string 1: ";
getline(cin, niz1);
cout << "string 2: ";
getline(cin, niz2);
for (int i = 0; i < niz1.length(); i++) {
for (int j = 0; j < niz2.length(); j++) {
if (niz1[i] == niz2[j])
niz3 += niz1[i];
}
}
set<char> unique(niz3.begin(), niz3.end());
niz3.assign(unique.begin(), unique.end());
cout << "Same letters are: " << niz3 << endl;
return 0;
}
Input:
string 1: adbcda
string 2: cdecd
Output:
Same letters are: cd
Also you may use just set
s plus set_intersection
standard function. This will solve your task in less time, in O(N*log(N))
time instead of your O(N^2)
time.
Variant 3:
Try it online!
#include <string>
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
string niz1, niz2, niz3 = "";
cout << "string 1: ";
getline(cin, niz1);
cout << "string 2: ";
getline(cin, niz2);
set<char> s1(niz1.begin(), niz1.end()), s2(niz2.begin(), niz2.end());
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), back_inserter(niz3));
cout << "Same letters are: " << niz3 << endl;
return 0;
}
Input:
string 1: adbcda
string 2: cdecd
Output:
Same letters are: cd
Instead of set
it is also possible to use unordered_set
, it will give even more faster algorithm especially for long strings, algorithm will have running time O(N)
compared to O(N * log(N))
for set
solution. The only drawback is that unlike for set
solution output of unordered_set
solution is unsorted (but unique) (unordered sets don't sort their data).
Variant 4:
Try it online!
#include <string>
#include <iostream>
#include <unordered_set>
#include <algorithm>
#include <iterator>
using namespace std;
int main() {
string niz1, niz2, niz3;
cout << "string 1: ";
getline(cin, niz1);
cout << "string 2: ";
getline(cin, niz2);
unordered_set<char> s1(niz1.begin(), niz1.end()), s2;
for (size_t i = 0; i < niz2.length(); ++i)
if (s1.count(niz2[i]))
s2.insert(niz2[i]);
niz3.assign(s2.begin(), s2.end());
cout << "Same letters are: " << niz3 << endl;
return 0;
}
Input:
string 1: adbcda
string 2: cdecd
Output:
Same letters are: dc
Also one more way is to use just plain for
loops like you did, without sets, but do extra block of loops in order to remove non-unique letters, like in code below. The only drawbacks of this loops method compared to sets method is that loops method runs slower and produces non-sorted output string.
Variant 5:
Try it online!
#include <string>
#include <iostream>
using namespace std;
int main() {
string niz1, niz2, niz3, niz4;
cout << "string 1: ";
getline(cin, niz1);
cout << "string 2: ";
getline(cin, niz2);
for (int i = 0; i < niz1.length(); ++i)
for (int j = 0; j < niz2.length(); ++j)
if (niz1[i] == niz2[j])
niz3 += niz1[i];
for (int i = 0; i < niz3.length(); ++i) {
bool exists = false;
for (int j = 0; j < niz4.length(); ++j)
if (niz4[j] == niz3[i]) {
exists = true;
break;
}
if (!exists)
niz4 += niz3[i];
}
cout << "Same letters are: " << niz4 << endl;
return 0;
}
Input:
string 1: adbcda
string 2: cdecd
Output:
Same letters are: dc