-3

can someone tell me what the program is doing here?

  if (*p1 == '\0' || *p2 == '\0') {
      return (*p2 == '\0') - (*p1 == '\0');
    }

am I correct if the program is comparing the pointerposition to the nullcharacter (last character) and if one of them is the case, then it returns the length of the pointer p2 at position nullcharacter, substracted from length of pointerposition p1 at the nullcharacter?

here is my complete program:

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;



int strcmp_ign_ws(const char *s1, const char *s2) {
    const char *p1 = s1, *p2 = s2;
    int count(0);

    while (true) {
    while (*p1 != '\0' && (*p1 == ' ')) p1++;
    while (*p2 != '\0' && (*p2 == ' ')) p2++;
    if (*p1 == '\0' || *p2 == '\0') {
      return (*p2 == '\0') - (*p1 == '\0');
    }
    if (*p1 != *p2) {
      count += (unsigned char)*p2 - (unsigned char)*p1;
    }
    p1++;
    p2++;
  }  
  return count;
}


int main() {

    char a[] = "Hallo Weltt";
    char b[] = "Hallo Welt";

    int result(0);

    result = strcmp_ign_ws(a,b);

    cout << result << endl;

    return 0;
}
MMMM
  • 3,320
  • 8
  • 43
  • 80

2 Answers2

1

If either of p1 or p2 points to the end of string (zero character, '\0') then it returns difference of the boolean expressions (p2=='\0' and p1=='\0').

p1=='\0' here means that the pointer points to the end of a string.

Both booleans will be promoted to int and substracted, returning 0, 1 or -1.

Return value 0 will then mean that both pointers point to the end of a string (i.e. the strings are the same).

Other values mean the strings are not the same (1 means string p1 is longer than p2, -1 means p2 is longer that p1).

On the side note, the rest of the function seems broken, value of count will never be used anywhere...

This question may be interesting for you.

EDIT: Assuming, you only want functionality of std::strcmp ignoring whitespaces, then you can write something like:

int strcmp_ign_ws(const char *p1, const char *p2) {
    while (true) {
        while (*p1 == ' ') p1++;
        while (*p2 == ' ') p2++;
        if (*p1 == '\0' || *p2 == '\0') {
            return (*p2 == '\0') - (*p1 == '\0');
        }
        if (*p1 != *p2) {
            return static_cast<int>(*p2) - static_cast<int>(*p1);
        }
        p1++;
        p2++;
    }
}

While this solution should work, you would be better off with using std::string as Jerry Coffin said. To keep semantics of this white-space ignoring string comparison, you can simply strip all white-spaces from both strings and then compare them by std::string::compare().

Community
  • 1
  • 1
v154c1
  • 1,698
  • 11
  • 19
1

What you have here is (IMO) a poorly designed function--it tries to return two entirely different kinds of information, in a way that would be ambiguous if it were sufficiently free of bugs to actually do what seems to have been intended.

The return statement you're looking at compares characters in each string to '\0' to see whether it's at the end of at least one of the strings. If it's at the end of at least one string, it returns -1, 0, or 1 to indicate which string is longer (ignoring white-space). (-1=second string longer, 1 = first string longer, 0 = strings same length).

It also counts up mismatches in the strings in count and has code intended to return the count of mismatches (but which can't ever actually do anything useful, since the only exit from the while (true) loop is via the first return).

As far as how to do roughly the same thing well, a lot depends on the rest of your code. Most code should probably use std::string for most strings. Rather than using the three-way result produced by this code, you should normally just use a bool result indicating whether some string A is longer than some string B. Nearly the only function that wants this three-way result is qsort -- if you're using that, you should usually switch to using std::sort instead (and it requires only a bool result).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • thanks, is there another way to compare length of the strings with pointers without that boolean method? – MMMM Dec 01 '13 at 18:51