If I have a program that needs to compare a lot of strings, what’s the best way to do it such that run time is at a minimum?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
strcmp()– compare two strings.are the strings to be compared.
gives the results of the comparison.
iis zero if the strings are identical.iis positive if strings1is greater than strings2, and is negative if strings2is greater than strings1. Comparisons of “greater than” and “less than” are made according to the ASCII collating sequence.strcmp()compares the strings1to the strings2. Both strings must be terminated by the usual ‘\0'character.strncmp()are the strings to be compared.
gives the number of characters to be examined.
gives the results of the comparison.
iis zero if the firstNcharacters of the strings are identical.iis positive if string “s1” is greater than strings2, and is negative if string “s2” is greater than strings1. Comparisons of “greater than” and “less than” are made according to the ASCII collating sequence.strncmp()compares the first N characters of the strings1to the firstNcharacters of the strings2. If one or both of the strings is shorter thanNcharacters (i.e. ifstrncmp()encounters a ‘\0'), comparisons will stop at that point. ThusNrepresents the maximum number of characters to be examined, not the exact number. (Note that ifNis zero,strncmp()will always return zero — no characters are checked, so no differences are found.)are the strings to be compared.
size_t N;
gives the number of characters to be examined.
gives the results of the comparison.
iis zero if the firstNcharacters of the strings are identical.iis positive if string “s1” is greater than strings2, and is negative if strings2is greater than strings1. Comparisons of “greater than” and “less than” are made according to the ASCII collating sequence.memcmp()compares the firstNcharacters of the string “s1” to the first N characters of the strings2.Unlike the function
strncmp(),memcmp()does not check for a'\0'terminating either string. Thus it examines a fullNcharacters, even if the strings are not actually that long.The function
wmemcmp()compares the size wide characters beginning at a1 against the size wide characters beginning at a2. The value returned is smaller than or larger than zero depending on whether the first differing wide character is a1 is smaller or larger than the corresponding character in a2.If the contents of the two blocks are equal,
wmemcmp()returns 0.On arbitrary arrays, the
memcmp()function is mostly useful for testing equality. It usually isn’t meaningful to do byte-wise ordering comparisons on arrays of things other than bytes. For example, a byte-wise comparison on the bytes that make up floating-point numbers isn’t likely to tell you anything about the relationship between the values of the floating-point numbers.The wcscmp function compares the wide character string ws1 against ws2. The value returned is smaller than or larger than zero depending on whether the first differing wide character is ws1 is smaller or larger than the corresponding character in ws2.
If the two strings are equal,
wcscmp()returns 0.A consequence of the ordering used by
wcscmp()is that if ws1 is an initial substring ofws2, then ws1 is considered to be “less than” ws2.wcscmp()does not take sorting conventions of the language the strings are written in into account. To get that one has to usewcscoll.This function is like
wcscmp(), except that differences in case are ignored. How uppercase and lowercase characters are related is determined by the currently selected locale. In the standard “C” locale the characters Ä and ä do not match but in a locale which regards these characters as parts of the alphabet they do match.strcmpi()comparesstring1andstring2without sensitivity to case. All alphabetic characters in the two arguments string1 and string2 are converted to lowercase before the comparison.The function operates on null-ended strings. The string arguments to the function are expected to contain a null character
'\0'marking the end of the string.strcmpi()returns a value indicating the relationship between the two strings , as followsLess than 0
string1less thanstring20
string1equivalent to string2Greater than 0
string1greater thanstring2.This function is like
strcmp(), except that differences in case are ignored. How uppercase and lowercase characters are related is determined by the currently selected locale. In the standard “C” locale the characters Ä and ä do not match but in a locale which regards these characters as parts of the alphabet they do match.This function is like
strncmp(), except that differences in case are ignored. Likestrcasecmp(), it is locale dependent how uppercase and lowercase characters are related.Which approach is best is certainly dependent upon your requirements.