1

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 "

  • *"is there a way I can convert the strings elements1 and elements2 into integer values and compare them?"* -- this is your question. Focus on converting to integer values, move the context later in the question, and drop the code that deals with "matching". – JaMiT Sep 05 '21 at 03:11
  • It might also help if you could distinguish your question from [Convert String with commas separated values to vector](https://stackoverflow.com/questions/35338705) and [Copying a set to comma-separated std::string](https://stackoverflow.com/questions/67837763) – JaMiT Sep 05 '21 at 03:14

1 Answers1

1

is there a way I can convert the strings elements1 and elements2 into integer values and compare them?

Yes, but your code has a bigger problem. Currently you are only looking for an occurrence of individual characters. This isn't going to work for numbers larger than one digit and is the reason your output includes a 3.

Having said that, two approaches spring to mind. You can use a combination of string::find and string::substr to get the sub strings between the commas. This will solve the problem I mentioned and you don't actually need to perform any type conversions in that case.

An alternative approach is to use a std::stringstream to parse the strings essentially converting them to arrays of integers. If each string is guaranteed to have no duplicates you could concatenate the strings and use a std::map to keep track of the number of occurrences of each integer.

would I have to convert the matched integers values back into a string?

This depends on your requirements. Though you wouldn't need to if you're just going to output the results.

matdon
  • 146
  • 6