Skip to main content
GameDev.net gamedev.net
🔒 Locked

String Compare Function

Started by chrpeter Nov 26, 2008 at 4:31 AM 2 replies 700+ views
Original Post
chrpeter
chrpeter
Hi. I am making my own string class and I do not want to use strcmp in my compare function. I understand that the function is suppose to return 0 if the strings letters are equal but the part I don't understand is how to check if the letters are alphabetically less or greater than if the strings aren't equal. The compare function is suppose to return -1 if the letter is alphabetically less than the other strings letter and a 1 if greater than the other strings letter. I am unsure how to compare the letters to see if they are alphabetically less or greater. Here is my code so far: int MyString::Compare(const MyString & aMyString) { int addUpStringElements = 0; if (_length == aMyString._length) { //iterate through the strings comparing each element for (int i = 0; i < _length; i++) { //if the elements are equal then add 1 if (_string == aMyString._string) { addUpStringElements++; } //if addupstringelements is the size of length then the strings are equal if (addUpStringElements == _length) { return 0; } } } I have the equal part done. I just need to know how to check alphabetical. Thanks.
Chris Peterson
Zipster
Zipster
Unless this is for learning purposes, you should be using std::string.

Otherwise, you can just compare the character values directly using standard operators (<, >, ==, etc.). As least in ASCII, the characters are laid out alphabetically by increasing value. You'll need to convert to the same case first if you want a case-insensitive comparison.
Ezbez
Ezbez
Well, your current idea is undefined in many cases. Which is alphabetically greater?

@hello
or
#hello

Hello
or
hello

helloWorld
or
hello

Here's what I would do, in psuedo-code:

int Compare( string a, string b ){//Note: I'd have this as a free function, rather than a membera = str1[0]b = str2[0]while( a != null and b != null ){    equals = Compare( a, b )    if( equals == 0 )    {        advance a;        advance b;    }        return equals;}//We only get here if they have been equal so farif( a == null  and b != null )    return -1; // a comes before belse if( a != null and b == null )    return 1; // b comes before aelse    return 0; // equal!}//And now the code for Compare, which compares two charactersint Compare( char a, char b ){//your code in here. //Can't really help you with this, //since we don't know the specifics of your alphabetical order//the easiest way is just this:if( a < b )    return -1;if( b > a )    return 1;else    return 0;//But that would put capitals before lowercases, //with some other characters in between//so you'll need to make some more complicated solution//unless  you're just sorting this for, say, a sorted map//but if you're showing this to end users,//don't use this method.}
DevFred
DevFred
Quote:
Original post by chrpeter
I do not want to use strcmp in my compare function.

Why not? strcmp basically boils down to something like this:
int strcmp(const char *a, const char *b){    // same current character?    while (*a == *b)    {        // if we have reached the end,        // both strings are the same        if (*a == 0) return 0;        // otherwise move on to the next character        ++a;        ++b;    }    // current character differs    return *a - *b;}

Can you do better than that by writing your own function?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.