Updated: April 19, 2023 |
Compare two strings, ignoring case, up to a given length
#include <strings.h> int strncasecmp( const char* str1, const char* str2, size_t n );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The strncasecmp() function compares up to n characters in two strings, specified by str1 and str2, ignoring the case of the characters.
The following code:
#include <stdio.h> #include <strings.h> #include <stdlib.h> void compare( const char *s1, const char *s2 ) { int retval; retval = strncasecmp( s1, s2, 3 ); if( retval > 0 ) { printf( "%s > %s\n", s1, s2 ); } else if( retval < 0 ) { printf( "%s < %s\n", s1, s2 ); } else { printf( "%s == %s\n", s1, s2 ); } } int main( void ) { char *str1 = "abcdefg"; char *str2 = "HIJ"; char *str3 = "Abc"; char *str4 = "aBCDEfg"; compare( str1, str2 ); compare( str1, str3 ); compare( str1, str4 ); compare( str1, str1 ); compare( str2, str2 ); compare( str2, str3 ); compare( str2, str4 ); compare( str2, str1 ); return EXIT_SUCCESS; }
produces output that looks like:
abcdefg < HIJ abcdefg == Abc abcdefg == aBCDEfg abcdefg == abcdefg HIJ == HIJ HIJ > Abc HIJ > aBCDEfg HIJ > abcdefg
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Yes |
Signal handler | Yes |
Thread | Yes |