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;
}