String Comparison Without STL (code review)

I have written a string comparison function. It works like I intended but I want a critic review. Took me a while to arrive at this so I'd like to know other ways that this could be done without STL support (like std::compare).

Meanwhile, here's my code:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

using namespace std;

// compare left strings, left param to right
// return 0 for equal, 1 for str1 first and -1 if str2 comes first
int compareLTR(string str1, string str2)
{
	unsigned int index = 0;	// for walking through the strings
	int comp = 0;	// holder for comparison result
	
	while(str1[index] == str2[index])
	{
		// condition to prevent infinite loop
		if(str1.length() == index && str2.length() == index)
			break;
		index++;
		
		// if current chars are not equal...
		if(str1[index] != str2[index])
		{
			// then check which leads which
			comp = ((int)str1[index] > (int)str2[index] ? 1:-1);
			break;
		}
		
		// if indeed the chars are equal...
		if(comp == 0)
		{
			// but str1 is shorter than str2...
			if(str1.length() == index && str2.length() > index)
				comp = -1;	// str2 wins
			// however, if str2 is shorter...
			if(str2.length() == index && str1.length() > index)
				comp = 1; // str1 wins
		}
	}
	
	return comp;
}

// just a test wrapper
void compare(string str1, string str2)
{	
	switch(compareLTR(str1, str2))
	{
		case -1:
			cout << str1 << " < " << str2;
			break;
		case 1:
			cout << str1 << " > " << str2;
			break;
		default:
			cout << str1 << " = " << str2;
			break;
	}
}

int main()
{
	// test cases
	compare("aaa", "aaa");
	cout << "\n";
	compare("aab", "aac");
	cout << "\n";
	compare("aab", "aaA");
	cout << "\n";
	compare("aaba", "a");
	cout << "\n";
	compare("aaba", "ab");
	cout << "\n";

	return 0;
}



which outputs:

1
2
3
4
5
aaa = aaa
aab < aac
aab > aaA
aaba > a
aaba < ab



Any possible improvements?
closed account (LA48b7Xj)
std:string has built in relational operators not sure why you would want to go to all this trouble.

http://www.cplusplus.com/reference/string/string/operators/
Last edited on
> I'd like to know other ways that this could be done without STL support

Here's one other way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int compare( const std::string& a, const std::string& b )
{
    std::size_t index = 0 ;
    const std::size_t min_sz = a.size() < b.size() ? a.size() : b.size() ;

    // get to the position of the first mismatched character (up to a max of min_sz)
    while( a[index] == b[index] && index < min_sz ) ++index ;

    // if mismatch occurred before min_sz (before the end of either string)
    if( index < min_sz ) return a[index] < b[index] ? -1 : +1 ;

    // all characters up to min_sz were matched
    if( a.size() == b.size() ) return 0 ; // if the strings are of equal length, they compare equal
    else return a.size() < b.size() ? -1 : +1 ; // otherwise, the shorter string is the smaller one
}
@krako I explicitly stated that I knew about it. The idea was to build mine, not implement.

@JLBorges thanks. I've seen a thing or two there.
closed account (LA48b7Xj)
Nah, you said without STL support.
Correct. Still, it's for practice, so I don't want to make use of inbuilt functions.
Topic archived. No new replies allowed.