my code is supposed to find the matched integer values from an input string with two sets of elements...my approach was to split the string elements into two elements strings elements1 and string elements2 using a [i] loop and a [j] loop to iterate through the elements to find to see which numbers match and if they doo store it in a matched string...but I notice that the comas and the spaces within the stings are also getting compared...is there a way I can convert the strings elements1 and elements2 into integer values and compare them? also, would I have to convert the matched integers values back into a string? an example would be
Input: {"1, 3, 4, 7, 13", "1, 2, 4, 13, 15"}
Output: 1,4,13
Please help me, I feel like I am close to solving this!
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
string FindIntersection(string strArr[], int arrLength)
{
// code goes here
bool num_matched;
string matched = "";
string elements1 = strArr[0];
string elements2 = strArr[1];
cout << "elemets 1: " << elements1 << '\n';
cout << "elemets 2: " << elements2 << '\n';
for (int i = 0; i < elements1.length(); i++)
{
num_matched = true;
char num1 = elements1[i];
for (int j = 0; j < elements2.length(); j++)
{
char num2 = elements2[j];
if (num1 == num2)
{
num_matched = true;
break;
}
else
{
num_matched = false;
}
}
if (num_matched == true)
{
matched += num1;
}
}
return matched;
// return strArr[0];
}
int main(void)
{
string A[] = { "1, 3, 4, 7, 13", "1, 2, 4, 13, 15" };
int arrLength = sizeof(A) / sizeof(*A);
cout << '\n' << "matchaed number in both elemets:" << '\n' << FindIntersection(A, arrLength);
return 0;
}
the current output i get is below: " elemets 1: 1, 3, 4, 7, 13 elemets 2: 1, 2, 4, 13, 15
matchaed number in both elemets: 1, 3, 4, , 13 "