You can use memcmp to compare the strings up to the point where one of them ends.
int strcmpNoTerminator ( const char * str1, const char * str2, size_t str1len, size_t str2len ) {
// Get the length of the shorter string
size_t len = str1len < str2len ? str1len : str2len;
// Compare the strings up until one ends
int cmp = memcmp(str1, str2, len);
// If they weren't equal, we've got our result
// If they are equal and the same length, they matched
if(cmp != 0 || str1len == str2len) {
return cmp;
}
// If they were equal but one continues on, the shorter string is
// lexicographically smaller
return str1len < str2len ? -1 : 1;
}
Note that this is if your char *s are actually not null terminated. In your example code, one
, two
, and three
are null terminated. I'm assuming that your question itself is correct, and not your example. If the example is correct, then your char *s are null terminated and your problem lies elsewhere, in which case we'd need to see more code to help.