The fastest way to strncmp() , which limits the length to be compared.
if (strncmp(sa, sb, length)==0) ...
This assumes that the length you are using is the maximum length of two lines. If a null-terminated string can be long, you must first compare the length.
if(strncmp(sa,sb, length)==0 && strlen(sa)<=length)
Note that strlen () is specifically checked after comparison to avoid unnecessarily iterating over all characters of the end line if the first characters do not match.
The final version:
if(strncmp(sa,sb, length)==0 && sa[length]==0)
Christophe Mar 11 '15 at 20:52 2015-03-11 20:52
source share