Name
strncat, strncmp, strnicmp, strncpy, strnset - Perform
operations on characters in strings.
Syntax
#include <string.h>
char *strncat(string1, string2, n)
char *string1;
const char *string2;
size_t n;
int strncmp(string1, string2, n)
const char *string1;
const char *string2;
size_t n;
int strnicmp(string1, string2, n)
const char *string1;
const char *string2;
size_t n;
char *strncpy(string1, string2, n)
char *string1;
const char *string2;
size_t n;
char *strnset(string, c, n)
char *string;
int c;
size_t n;
Description
The strncat, strncmp, strnicmp, strncpy, and strnset
functions operate on, at most, the first n characters of
null-terminated strings.
The strncat function appends, at most, the first n
characters of string2 to string1, terminates the resulting
string with a null character (\0), and returns a pointer to
the concatenated string (string1). If n is greater than the
length of string2, the length of string2 is used in place of
n.
The strncmp function compares, at most, the first n
characters of string1 and string2 lexicographically and
returns a value indicating the relationship between the
substrings, as listed below:
Value Meaning
< 0 substring1 less than substring2
= 0 substring1 equivalent to substring2
> 0 substring1 greater than substring2
The strnicmp function is a case-insensitive version of
strncmp.
The strncpy function copies exactly n characters of string2
to string1 and returns string1. If n is less than the length
of string2, a null character (\0) is not appended
automatically to the copied string. If n is greater than the
length of string2, the string1 result is padded with null
characters (\0) up to length n.
Note that the behavior of strncpy is undefined if the
address ranges of string1 and string2 overlap. The strnicmp
and strnset functions are not part of the ANSI definition
but are instead Microsoft extensions to it, and should not
be used where ANSI portability is desired.
The strnset function sets, at most, the first n characters
of string to the character c and returns a pointer to the
altered string. If n is greater than the length of string,
the length of string is used in place of n.
Return Value
The return values for these functions are described above.
See Also
strcat(DOS), strcmp(DOS), strcpy(DOS), strset(DOS)
Example
#include <string.h> #include <stdio.h>
char string[100] = "XYZabbc This is a string!"; char
copy[100] = "This is a different string"; char *result; char
suffix[100] = " this is even more string.."; int numresult;
main() {
/* Combine strings with no more than */
/* 100 characters of suffix: */
printf("String before = %s\n", string);
result = strncat(string,suffix,100);
printf("String after = %s\n", string);
/* Determine ordering of two strings */
/* but only consider first 7 chars: */
strcpy(string, "programming");
numresult = strncmp(string,"program",7);
printf("\"%s\" is %s \"%s\"\n", string,
numresult ? (numresult > 0 ?
"greater than" : "less than") : "equal to",
"program");
/* Copy at most 99 chars of "string" */
printf("%s \"%s\"\n", copy, string);
result = strncpy(copy,string,99);
/* Null terminate the result */
copy[99] = '\0';
printf("%s %s \n", copy, string);
/* Set not more than 4 characters of a */
/* string to be x's: */
result = strnset("computer",'x',4);
/* Result is now "xxxxuter". */
printf( "%s\n", result );
}
This program demonstrates the uses of the strncat, strncmp,
strnicmp, and strnset functions.
(printed 6/18/89)