Let's say we have the two strings FOOBAR and BFORAO. While not really equal as strings, they contain exactly the same characters and according to my needs, I can treat them as equal. And I will be calling them "equal" from now on to avoid typing again and again long sentences.

I need a fast way to check if two strings are equal.

As far as I know, the strspn() function will take two strings, and if all the characters of string2 appear in string1, it will return string1's length. Good. But what if I compare FOOBAR against BAR? Indeed, every character from string2 appears in string1... but... not vice-versa. Hm, I could possible run strspn() for one more time, this time reverted (a.k.a. checking BAR against FOOBAR). If the two outputs are the same, bingo. The two strings are equal.

But is it efficient enough? I need to do this for more than 400K strings, and although I have a dual-core with 6GB of RAM, the process needs to be completed in a relatively small time-frame. I haven't actually started on this program yet, I'm still looking for the most efficient way to accomplishe the task I've described.

Any ideas?

Maybe evaluating each string "by hand" (running tests inside nested loops against every individual character, etc.) would be faster than the solution I've thought of using the string header?

Sort both strings so thzt the characgters appear in the same order in both strings, then eliminate duplicate characters. After that, convert both strings to the same case, e.g. upper or lower, doesn't matter which. Finally call strcmp() to compare them.

Another method is to count the number of occurrences of each character in each string, then compare the two results. Create two int arrays, one for each string. The size of these two arrays is the same, regardless of the length of the two strings. For example

int count1[255] = {0};
int count2[255] = {0};
char s1[] = "FOOBAR";
char s2[] = "BFORAO";
int i;
for(i = 0; s1[i]; i++)
   count1[s1[i]]++;

for(i = 0; s2[i]; i++)
   count2[s2[i]]++;

// now compare the results
if( memcmp(count1,count2,sizeof(count1)) == 0)
{
   // the two are the same
}

There could be variations of the above, for example if you don't care how many times the same letter appears then just set the array element to 1, e.g. count1[s1[i]] = 1. To make case insensitive compare: count1[tolower(s1[i])] = 1;

strspn() will return NULL if you try to compare strings like FOOBAR and BFORAO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.