Comparing several strings

Hello,

I am running into a small problem. Let's say I have 32 strings declared such as:
string strArray[33];

So I have the array and I want to compare all of them and at the end get the ones that are equal.

For example (first array element is a dummy value):
1
2
3
4
strArray[1] = "Hello";
strArray[2] = "Happy birthday";
strArray[3] = "Hello";
strArray[4] = "Good morning";


At the end i want for example to print something like:
Strings #1, #3 are equal.

NOTE: I know that how to compare two strings but the thing is, i donno whats inside those strings and second i have an array of 32 strings.

Any help is appreciated.
Thank you in advance.

Last edited on
Do you need to know how ofhen is each string encountered in the array?
If you can then why not. But if it's not included then it won't make a difference to me. I can always add it later on.
Last edited on
Anyone? I know this is a bit hard to do, and that's why i came here for help.
Last edited on
I have tried to it this way:
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
// Values: 1 5 7 8 9 5 4 1 2  5  4  5  7  8  9  6  2  4  5  2  1  5  4  7  8  5  2  1  4  5  7  1
// snOfvl: 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

// Checked: 1;8;21;28;32;2;6;10;12;19;22;26;30;3;13;24;31;4;14;25;5;15;7;11;18;23;29;9;17;20;27;16

// Group1: (#1)  1; 8;21;28;32;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;
// Group2: (#5)  2; 6;10;12;19;22;26;30;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;
// Group3: (#7)  3;13;24;31;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;
// Group4: (#8)  4;14;25;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;
// Group5: (#9)  5;15;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;
// Group6: (#4)  7;11;18;23;29;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;
// Group7: (#2)  9;17;20;27;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;
// Group8: (#6) 16;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;  ;

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int snOfIp[33] = {0,1,5,7,8,9,5,4,1,2,5,4,5,7,8,9,6,2,4,5,2,1,5,4,7,8,5,2,1,4,5,7,1};
	bool checkedSn[33];
	int groupOfSimilarIP[33][33];
	int compareTo=1;
	
	for(int y=1; y<33; y++) {
		for(int x=1; x<33; x++) {
			if(x!=compareTo && checkedSn[compareTo]){
				groupOfSimilarIP[compareTo][y] = snOfIp[compareTo];
				checkedSn[compareTo] = true;
				
				if(snOfIp[compareTo] == snOfIp[x]) {
					groupOfSimilarIP[x][y] == snOfIp[x];
					checkedSn[x] = true;
				}
			}
		compareTo++;
		}
	}
	
	for(int j=1; j<33; j++) {
		cout << "Group " << j << ": ";
		for(int i=1; i<33; i++) {
			cout << /*setfill(0) <<*/ setw(2) << groupOfSimilarIP[i][j] << ";";
		}
		cout << endl;
	}
	
	return 0;
}


In the comments, is what you should get as result, but when i run the code, it doesn't go anywhere near it.

Any help?
Last edited on
No one? :O

Big forum, thousands of users and no one has an answer?

My bad luck i guess :(
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
#include <iostream>
#include <type_traits>
#include <map>
#include <vector>
#include <functional>
#include <iostream>
#include <string>

void print_duplicates_simple( const std::string* array, std::size_t n )
{
    std::map< std::string, std::vector<std::size_t> > map ;
    for( std::size_t i = 0 ; i < n ; ++i ) map[ array[i] ].push_back(i) ;

    for( const auto& pair : map )
    {
        const auto& vector = pair.second ;
        if( vector.size() > 1 )
        {
            for( std::size_t pos : vector ) std::cout << '#' << pos << ", " ;
            std::cout << "are equal (" << pair.first << ")\n" ;
        }
    }
}

template< typename ITERATOR >
void print_duplicates( ITERATOR begin, ITERATOR end, std::ostream& stm = std::cout )
{
    typedef typename std::add_const<
                    typename std::iterator_traits<ITERATOR>::value_type >::type T ;
    typedef typename std::iterator_traits<ITERATOR>::difference_type pos_type ;

    std::map< std::reference_wrapper<T>, std::vector<pos_type>, std::less<T> > map ;
    const auto N = std::distance( begin, end ) ;
    for( pos_type i = 0 ; i < N ; ++i, ++begin ) map[ *begin ].push_back(i) ;

    for( const auto& pair : map )
    {
        const auto& vector = pair.second ;
        if( vector.size() > 1 )
        {
            for( std::size_t pos : vector ) std::cout << '#' << pos << ", " ;
            std::cout << "are equal (" << pair.first.get() << ")\n" ;
        }
    }
}

int main()
{
    std::string a[] = { "abc", "defg", "abc", "hijklm", "defg", "abc", "nopq", "abc" } ;
    print_duplicates_simple( a, sizeof(a)/sizeof(*a) ) ;
    print_duplicates( std::begin(a), std::end(a) ) ;
}
Thank you, i was able to get it working.
Last edited on
Topic archived. No new replies allowed.