Re: Question on strncmp / strnicmp use



On Jan 16, 5:52 pm, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:
vipps...@xxxxxxxxx writes:
On Jan 16, 5:05 pm, C. J. Clegg <answer.in.newsgr...@xxxxxxxxx> wrote:
I have two null-terminated character strings, str1 and str2.  I don't
know which one is longer, but I want to know if the shortest one
(whichever one that might be) matches the first part of the longer
one.

I can say:

if ( strncmp( str1, str2, strlen( str2 ) ) == 0 ||
     strncmp( str2, str1, strlen( str1 ) )  == 0 )

but that seems a bit kludgy and inelegant.

Is there a better way?

Writing your own function is the most efficient way, and also a good
solution.
It's more efficient than strncmp, because for strncmp you have to pass
the length too, which you usually compute with strlen, which means you
iterate the string one more time than needed.

Very similar tests are need when one writes this function.  You have
to test the characters from both strings and increment two pointers.
strlen might well be kinder on the cache and might use a single
machine instruction.  Anyway, my point is that I, for one, am
constantly amazed by how often my intuition about efficiency wrong.
This means that you could well be right, of course!

Here's what I have in mind:

strlen can't be less than O(n) (optimizations that, say, check 4
elements at once are still O(n))
mycomparison can't be less than O(n).

Using just mycomparison instead of strlen + mycomparison is one less O
(n) function called, so in that way, I'm correct.
You mention increment of pointers and comparison, but those operations
are O(1).

All that is not supported by the standard which doesn't even mention
the concept of efficiency/time of operation, but I hope you can see
what I'm trying to say.

int mycomparison(const char *s1, const char *s2) {
    for(; *s1 == *s2; s1++, s2++);

This loop has UB.  If the strings are the same length it starts to
look beyond the nulls.  Of course, this may be well-defined but, in
general, it is not safe.

Yes, you are right. Fix: (I hope)

for(; *s1 == *s2; s1++, s2++) if(*s1 == 0) break;


.



Relevant Pages

  • Re: A taxonomy of types
    ... however, elsewhere in my project (off in the dynamic typesystem, ...), I ... (since I am using NULL-terminated strings), and so I have used U+10FFFF ... remember, C also has things like arrays, funtion pointers, nestable ... int RIL_TypeSmallIntP; ...
    (comp.lang.misc)
  • Re: new IL: C (sort of...).
    ... only for "recent" Pascals, ... far pointers weren't really limited, ... in my compiler, I made wchar_t a builtin type (in most cases, aliased to ... I could very well include builtin "managed strings" in the new IL. ...
    (comp.lang.misc)
  • Re: HeapFree() Failing to deallocate string
    ... I've been able to recreate and isolate the problem with HeapFree(), ... in this simplified example is just a pointer to 16 bytes to store pointers ... the szCaption strings need to be copied to the pointed to ... strings for which storage needs to be allocated, ...
    (microsoft.public.windowsce.embedded)
  • Re: Increasing efficiency in C
    ... >> The representation of a string in C is the sequence of characters, ... strings, they are passed the addresses of strings. ... supports pointers the way it does. ... Competent programmers make mistakes, too. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)