Your s_compare() function is having undefined behaviour because it end up dereferencing the NULL pointer if user pass a valid or empty string to str1 and NULL to str2 as arguments to s_compare() function, like this:
s_compare ("abc", NULL);
s_compare ("", NULL);
Both of these calls will result in undefined behaviour.
To cover these cases:
- First string is longer.
- Second string is longer.
- Empty strings.
no need to call strlen() and get the length of string. To find out whether strings are same or not, you can use the fact that, in C, strings are actually one-dimensional array of characters terminated by a null character \0. Just iterate over them then either their character at a specific position may be different, or if their size is different and if the long string will have initial characters same as short string till the length of short string then the position at which the short string will have null character, at that position, long string will have some other character. In both cases, the strings are not same.
Since the function s_compare() is not supposed to modify the strings passed as arguments, you should declare the both the pointer parameters const.
Implementation:
bool s_compare (const char* str1, const char* str2) {
// if its the same pointer can return immediately
// also covers the case where both are null pointers
if (str1 == str2) {
return true;
}
// if one of them is null and other is not strings are not same
if ((str1 == NULL) || (str2 == NULL)) {
return false;
}
size_t i = 0;
// iterater over each character of string str1
while (str1[i]) {
// if any of the character in str1 and str2, at position i,
// is different that means strings are not same
if (str1[i] != str2[i]) {
return false;
}
++i;
}
// we reached here that means str1 is iterated till
// null character and str1 ith character is null character.
// So, if the str2 ith character is also null
// character than strings are same otherwise not
return str2[i] == '\0' ? true : false;
}
Driver program:
int main (void) {
printf ("Compare NULL and NULL : %d\n", s_compare (NULL, NULL));
printf ("Compare \"abc\" and NULL : %d\n", s_compare ("abc", NULL));
printf ("Compare \"\" and NULL : %d\n", s_compare ("", NULL));
printf ("Compare NULL and \"\" : %d\n", s_compare (NULL, ""));
char s1[10] = {0};
char s2[10] = {0};
printf ("Compare \"%s\" and \"%s\" : %d\n", s1, s2, s_compare (s1, s2));
strcpy (s1, "ABC");
strcpy (s2, "ABC");
printf ("Compare \"%s\" and \"%s\" : %d\n", s1, s2, s_compare (s1, s2));
strcpy (s1, "ab");
strcpy (s2, "ayz");
printf ("Compare \"%s\" and \"%s\" : %d\n", s1, s2, s_compare (s1, s2));
return 0;
}
Output:
Compare NULL and NULL : 1
Compare "abc" and NULL : 0
Compare "" and NULL : 0
Compare NULL and "" : 0
Compare "" and "" : 1
Compare "ABC" and "ABC" : 1
Compare "ab" and "ayz" : 0
str2has the same length asstr1to stop the loop unnecessarily starting if both strings do not have the same length: sincelenholds the length ofstr1, you can check ifstr2[len]holds'\0'str2[len]?