Jump to content
  • Advertisement
Sign in to follow this  
chrpeter

String Compare Function

This topic is 3616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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 member
a = 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 far
if( a == null and b != null )
return -1; // a comes before b
else if( a != null and b == null )
return 1; // b comes before a
else
return 0; // equal!

}

//And now the code for Compare, which compares two characters
int 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.
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!