jake
jake

Reputation: 33

String Comparison without compare()

I'm currently doing an assignment that requires us to create our own library for string comparison without using compare(), etc.

I got it to work, but during my research I created a bool function for character compare and return values.

It needs to work as if it returns like compare(), where 0 = strings are equal and 0 > or 0 < for not equal instead of true or false like I currently set it up to be.

I tried to change the bool functions to int but now when I run the program that was correctly returning strings are equal, it's showing not equal.

Header code:

bool compare_char(char &c1, char &c2)
{
    if (c1 == c2)
        return true;
    else if (toupper(c1) == toupper(c2))
        return true;
    else
        return false;
}

bool insensitive_string_comparision(string &string_one, string &string_two)
{
    return ((string_one.size() ==  string_two.size()) &&
        equal(string_one.begin(), string_one.end(), string_two.begin(), &compare_char));
}

string remove_spaces(string string)  
{ 
    string.erase(remove(string.begin(), string.end(), ' '), string.end()); 
    return string; 
} 
string remove_punctuation(string string)
{
    for (size_t i = 0, len = string.size(); i < len; ++i)
    {
        if (ispunct(string[i]))
        {
            string.erase(i--, 1);
            len = string.size();
        }
    }
    return string;

Int header changes

int compare_char(char &c1, char &c2)
{
    if (c1 == c2)
        return 0;
    else if (toupper(c1) == toupper(c2))
        return 0;
    else if (toupper(c1) > toupper(c2))
        return -1;
    else if (toupper(c1) < toupper(c2))
        return 1;
}

int insensitive_string_comparision(string &string_one, string &string_two)
{
    return ((string_one.size() ==  string_two.size()) &&
        equal(string_one.begin(), string_one.end(), string_two.begin(), &compare_char));
}

Int main changes

int result = insensitive_string_comparision(string_one, string_two);

if (result == 0)                
   cout << "Both Strings are equal." << endl;
else (result == 1 || result == -1)
   cout << "Both Strings are not equal." << endl;
return 0;

I feel like I'm going to have to redesign the entire function to return the value that is similar to compare().

I'm assuming bool was the wrong decision to begin with? Where should I go moving forward to return a correct value?

Upvotes: 1

Views: 331

Answers (1)

Ace shinigami
Ace shinigami

Reputation: 1484

In your question you are not entirely clear about how you want to compare the strings, but I made some assumptions based on your example code. You can fix your problem by writing insensitive_string_comparision like:

  int insensitive_string_comparision(string &string_one, string &string_two) {
    int len_one = string_one.length();
    int len_two = string_two.length();
    int len_comparison = 0;
    if (len_one > len_two) {
      len_comparison = -1;
    } else if (len_one < len_two) {
      len_comparison = 1;
    } 
    int minlen = (len_comparison == -1) ? len_one : len_two;

    for (int i = 0; i < minlen; i++) {
      int order = compare_char(string_one[i], string_two[i]);
      if (order != 0) {
        return order;
      }
    }

    return len_comparison;
  }

I'd also recommend turning on warnings on your compiler. You don't need to put some of your return statements in else blocks.

Upvotes: 1

Related Questions