strcasecmp() — Compare Strings without Case Sensitivity

Format

#include <strings.h>
int srtcasecmp(const char *string1, const char *string2);

Language Level: XPG4

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE category of the current locale. This function is not available when LOCALETYPE(*CLD) is specified on the compilation command. For more information, see Understanding CCSIDs and Locales.

Description

The strcasecmp() function compares string1 and string2 without sensitivity to case. All alphabetic characters in string1 and string2 are converted to lowercase before comparison.

The strcasecmp() function operates on null terminated strings. The string arguments to the function are expected to contain a null character ('\0') marking the end of the string.

Return Value

The strcasecmp() function returns a value indicating the relationship between the two strings, as follows:

Table 6. Return values of strcasecmp()
Value Meaning
Less than 0 string1 less than string2
0 string1 equivalent to string2
Greater than 0 string1 greater than string2

Example that uses strcasecmp()

This example uses strcasecmp() to compare two strings.

#include <stdio.h>
#include <strings.h>

int main(void)
{
  char_t *str1 = "STRING";
  char_t *str2 = "string";
  int result;

  result = strcasecmp(str1, str2);

  if (result == 0)
    printf("Strings compared equal.\n");
  else if (result < 0)
    printf("\"%s\" is less than \"%s\".\n", str1, str2);
  else
    printf("\"%s\" is greater than \"%s\".\n", str1, str2);

  return 0;
}

/********  The output should be similar to: ***************

Strings compared equal.

***********************************/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]