Chapter Contents

Previous

Next
strncasecmp

strncasecmp



Compare Portions of two strings, ignoring differences in case

Portability: SAS/C


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <strings.h>

int strncasecmp( const char *str1, const char *str2, size_t maxlen );


DESCRIPTION

strncasecmp compares, while ignoring differences in case, two character strings specified by str1 and str2. The comparison is performed using the standard EBCDIC collating sequence with the exception that differences in case are ignored. The return value has the same relationship to 0 as str1 has to str2. If the two strings are equal up to the point at which one terminates, that is, contains a null character, the longer string is considered greater. If maxlen characters are inspected from each string and no inequality is detected, the strings are considered equal. Note that case differences are determined in a locale- specific manner.


RETURN VALUE

strncasecmp returns 0 if the two strings are equal, an integer less than 0 if str1 compares less than str2, or an integer greater than 0 if str1 compares greater than str2, within the first maxlen characters. No other assumptions should be made about the value returned by strncasecmp.


CAUTION

If the maxlen value is specified as 0, a result of 0 is returned. If the value is a negative integer, it is interpreted as a very large unsigned integer value. This may cause a protection or addressing exception, but this is unlikely because comparsion ceases as soon as unequal characters are found.


IMPLEMENTATION

strncasecmp is functionally equivalent to strncmp, except that each byte is converted to lowercase before the comparison is performed.


EXAMPLE

#include <strings.h>

int main(void)
{
   int ch;
   size_t num;
   char lwr_alpha[] = "abcdefghijklmnopqrstuvwxyz";
   char upr_alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   char mix_alpha[] = "AbCdEfGhIjKlMnOpQrStUvWxYz";

   for (num = 1; num <= strlen(lwr_alpha); num++)
   {
     ch = strncasecmp(lwr_alpha, mix_alpha, num);
     if ( ch != 0 )
     {
        exit(EXIT_FAILURE);
     }
   }
   for (num = 1; num <= strlen(upr_alpha); num++)
   {
     ch = strncasecmp(upr_alpha, mix_alpha, num);
     if ( ch != 0 )
     {
        exit(EXIT_FAILURE);
     }
   }
   exit(EXIT_SUCCESS);
}


RELATED FUNCTIONS

strcmp, memcasecmp, memcmp, strcasecmp, strcmp


SEE ALSO

"String Utility Functions" in Chapter 2, "Function Categories."


Chapter Contents

Previous

Next

Top of Page

Copyright © 2004 by SAS Institute Inc., Cary, NC, USA. All rights reserved.