Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:50:00+00:00 2026-06-01T23:50:00+00:00

If I have a program that needs to compare a lot of strings, what’s

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:50:02+00:00Added an answer on June 1, 2026 at 11:50 pm

    strcmp() – compare two strings.

    const char *s1, *s2;
    

    are the strings to be compared.

    int i;
    i = strcmp( s1, s2 );
    

    gives the results of the comparison. i is zero if the strings are identical. i is positive if string s1 is greater than string s2, and is negative if string s2 is greater than string s1. Comparisons of “greater than” and “less than” are made according to the ASCII collating sequence.

    strcmp() compares the string s1 to the string s2. Both strings must be terminated by the usual ‘\0' character.


    strncmp()

    const char *s1, *s2;
    

    are the strings to be compared.

    size_t N;
    

    gives the number of characters to be examined.

    int i;
    i = strncmp( s1, s2, N );
    

    gives the results of the comparison. i is zero if the first N characters of the strings are identical. i is positive if string “s1” is greater than string s2, and is negative if string “s2” is greater than string s1. Comparisons of “greater than” and “less than” are made according to the ASCII collating sequence.

    strncmp() compares the first N characters of the string s1 to the first N characters of the string s2. If one or both of the strings is shorter than N characters (i.e. if strncmp() encounters a ‘\0'), comparisons will stop at that point. Thus N represents the maximum number of characters to be examined, not the exact number. (Note that if N is zero, strncmp() will always return zero — no characters are checked, so no differences are found.)


    memcmp()
    const void *s1, *s2;
    

    are the strings to be compared.
    size_t N;

    gives the number of characters to be examined.

    int i;
    i = memcmp( s1, s2, N );
    

    gives the results of the comparison. i is zero if the first N characters of the strings are identical. i is positive if string “s1” is greater than string s2, and is negative if string s2 is greater than string s1. Comparisons of “greater than” and “less than” are made according to the ASCII collating sequence.

    memcmp() compares the first N characters of the string “s1” to the first N characters of the string s2.

    Unlike the function strncmp(), memcmp() does not check for a '\0' terminating either string. Thus it examines a full N characters, even if the strings are not actually that long.


    wmemcmp()
    int wmemcmp(const wchar_t *a1, const wchar_t *a2, size_t size);
    

    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.


    wcscmp()
    int wcscmp(const wchar_t *ws1, const wchar_t *ws2);
    

    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 of ws2, 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 use wcscoll.


    wcscasecmp()
    int wcscasecmp(const wchar_t *ws1, const wchar_T *ws2)
    

    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()
    
    int strcmpi(const char *string1, const char *string2);
    

    strcmpi() compares string1 and string2 without 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 follows

    Less than 0 string1 less than string2

    0 string1 equivalent to string2

    Greater than 0 string1 greater than string2.


    strcasecmp()
    int strcasecmp(const char *s1, const char *s2);
    

    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.


    strncasecmp()
    int strncasecmp(const char *s1, const char *s2, size_t n);
    

    This function is like strncmp(), except that differences in case are ignored. Like strcasecmp(), it is locale dependent how uppercase and lowercase characters are related.


    Which approach is best is certainly dependent upon your requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that needs a lot of memory and want to set
I have a program that needs several third-party libraries, and at the moment it
I have a Flash program that needs to make url requests to send data
In an embedded program I have a screen object that needs to manage a
I have a legacy program that I need to run with the extension of
I'm currently writing a program that needs to compare each file in an ArrayList
I have a program that I need to be able to search a file
I have a program that is written in Ada, and I need to compile
I have a program that looks like this. I need to consistently write something
I have the need to use a Stack-like data structure for a program that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.