25.12.2013 Views

string(3C) string(3C) - HP

string(3C) string(3C) - HP

string(3C) string(3C) - HP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>string</strong>(<strong>3C</strong>)<br />

<strong>string</strong>(<strong>3C</strong>)<br />

NAME<br />

<strong>string</strong>: strcasecmp(), strcat(), strchr(), strcmp(), strcoll(), strcpy(), strcspn(), strdup(), strlen(),<br />

strncasecmp(), strncat(), strncmp(), strncpy(), strpbrk(), strrchr(), strrstr(), strspn(), strstr(), strtok(),<br />

strtok_r(), strxfrm(), index(), rindex() - character <strong>string</strong> operations<br />

SYNOPSIS<br />

#include <br />

#include <br />

char *strcat(char *__restrict s1, const char *__restrict s2);<br />

char *strncat(char *__restrict s1, const char *__restrict s2, size_t n);<br />

int strcmp(const char *s1, const char *s2);<br />

int strncmp(const char *s1, const char *s2, size_t n);<br />

int strcasecmp(const char *s1, const char *s2);<br />

int strncasecmp(const char *s1, const char *s2, size_t n);<br />

char *strcpy(char *__restrict s1, const char *__restrict s2);<br />

char *strncpy(char *__restrict s1, const char *__restrict s2, size_t n);<br />

char *strdup(const char *s);<br />

size_t strlen(const char *s);<br />

char *strchr(const char *s, int c);<br />

char *strrchr(const char *s, int c);<br />

char *strpbrk(const char *s1, const char *s2);<br />

size_t strspn(const char *s1, const char *s2);<br />

size_t strcspn(const char *s1, const char *s2);<br />

char *strstr(const char *s1, const char *s2);<br />

char *strrstr(const char *s1, const char *s2);<br />

char *strtok(char *__restrict s1, const char *__restrict s2);<br />

char *strtok_r(char *s1, const char *s2, char **last);<br />

int strcoll(const char *s1, const char *s2);<br />

size_t strxfrm(char *__restrict s1, const char *__restrict s2, size_t n);<br />

char *index(const char *s, int c);<br />

char *rindex(const char *s, int c);<br />

s<br />

Remarks<br />

All functions except index() and rindex() are declared in both headers, so only one of the two<br />

headers needs to be included.<br />

The functions index() and rindex() are declared only in . They are provided solely<br />

for portability of BSD applications, and are not recommended for new applications where portability is<br />

important. For portable applications, use , strchr(), and strrchr() instead.<br />

DESCRIPTION<br />

Arguments s1, s2, and s point to <strong>string</strong>s (arrays of characters terminated by a null byte).<br />

Definitions for all these functions, the type size_t, and the constant NULL are provided in the<br />

header.<br />

strcat() Appends a copy of <strong>string</strong> s2 to the end of <strong>string</strong> s1. strncat() appends a maximum<br />

of n characters. It copies fewer if s2 is shorter than n characters. Each returns a pointer<br />

to the null-terminated result (the value of s1).<br />

strcmp()<br />

Compares its arguments and returns an integer less than, equal to, or greater than zero,<br />

depending on whether s1 is lexicographically less than, equal to, or greater than s2. The<br />

comparison of corresponding characters is done as if the type of the characters were<br />

<strong>HP</strong>-UX 11i Version 3: September 2010 − 1 − Hewlett-Packard Company 1


<strong>string</strong>(<strong>3C</strong>)<br />

<strong>string</strong>(<strong>3C</strong>)<br />

s<br />

unsigned char. Null pointer values for s1 and s2 are treated the same as pointers to<br />

empty <strong>string</strong>s. strncmp() makes the same comparison but examines a maximum of n<br />

characters (n less than or equal to zero yields equality). strcasecmp() and<br />

strncasecmp() are identical in function to strcmp() and strncmp() respectively,<br />

but characters are folded by _tolower() (see conv(<strong>3C</strong>)) prior to comparison.<br />

The returned lexicographic difference reflects the folding to lowercase.<br />

strcpy() Copies <strong>string</strong> s2 to s1, stopping after the null byte has been copied. strncpy() copies<br />

exactly n characters, truncating s2 or adding null bytes to s1 if necessary, until a total of<br />

n have been written. The result is not null-terminated if the length of s2 is n or more.<br />

Each function returns s1. Note that strncpy() should not be used to copy n bytes of<br />

an arbitrary structure. If that structure contains a null byte anywhere, strncpy()<br />

copies fewer than n bytes from the source to the destination and fills the remainder with<br />

null bytes. Use the memcpy() function (see memory(<strong>3C</strong>)) to copy arbitrary binary data.<br />

strdup() Returns a pointer to a new <strong>string</strong> which is a duplicate of the <strong>string</strong> to which s1 points.<br />

The space for the new <strong>string</strong> is obtained using the malloc() function (see malloc (<strong>3C</strong>)).<br />

strlen() Returns the number of characters in s, not including the terminating null byte.<br />

strchr() (strrchr()) Returns a pointer to the first (last) occurrence of character c in <strong>string</strong> s, or<br />

a null pointer if c does not occur in the <strong>string</strong>. The null byte terminating a <strong>string</strong> is considered<br />

to be part of the <strong>string</strong>. index() (rindex()) is identical to strchr()<br />

(strrchr()), and is provided solely for portability of BSD applications.<br />

strpbrk() Returns a pointer to the first occurrence in <strong>string</strong> s1 of any character from <strong>string</strong> s2, ora<br />

null pointer if no character from s2 exists in s1.<br />

strspn() (strcspn()) Returns the length of the maximum initial segment of <strong>string</strong> s1, which<br />

consists entirely of characters from (not from) <strong>string</strong> s2.<br />

strstr() (strrstr()) Returns a pointer to the first (last) occurrence of <strong>string</strong> s2 in <strong>string</strong> s1, or<br />

a NULL pointer if s2 does not occur in the <strong>string</strong>. If s2 points to a <strong>string</strong> of zero length,<br />

strstr() (strrstr()) returns s1.<br />

strtok() Considers the <strong>string</strong> s1 to consist of a sequence of zero or more text tokens separated by<br />

spans of one or more characters from the separator <strong>string</strong> s2. The first call (with a nonnull<br />

pointer s1 specified) returns a pointer to the first character of the first token, and<br />

writes a null byte into s1 immediately following the returned token. The function keeps<br />

track of its position in the <strong>string</strong> s1 between separate calls, so that subsequent calls made<br />

with the first argument a null pointer work through the <strong>string</strong> immediately following that<br />

token. In this way subsequent calls work through the <strong>string</strong> s1 until no tokens remain.<br />

The separator <strong>string</strong> s2 can be different from call to call. When no token remains in s1, a<br />

null pointer is returned.<br />

strtok_r() is identical to strtok(), except that it expects to be passed the address of a character<br />

<strong>string</strong> pointer as the third argument. It will use this argument to keep track of the<br />

current position in the <strong>string</strong> being searched. It returns a pointer to the current token in<br />

the <strong>string</strong> or a NULL value if there are no more tokens.<br />

strcoll() Returns an integer greater than, equal to, or less than zero, according to whether the<br />

<strong>string</strong> pointed to by s1 is greater than, equal to, or less than the <strong>string</strong> pointed to by s2.<br />

The comparison is based on <strong>string</strong>s interpreted as appropriate to the program’s locale (see<br />

Locale below). In the ‘‘C’’ locale strcoll() works like strcmp().<br />

strxfrm() Transforms the <strong>string</strong> pointed to by s2 and places the resulting <strong>string</strong> into the array<br />

pointed to by s1. The transformation is such that if the strcmp() function is applied<br />

to two transformed <strong>string</strong>s, it returns a value greater than, equal to, or less than zero,<br />

corresponding to the result of the strcoll() function applied to the same two original<br />

<strong>string</strong>s. No more than n bytes are placed into the resulting <strong>string</strong>, including the terminating<br />

null character. If the transformed <strong>string</strong> fits in no more than n bytes, the<br />

length of the resulting <strong>string</strong> is returned (not including the terminating null character).<br />

Otherwise the return value is the number of bytes that the s1 <strong>string</strong> would occupy (not<br />

including the terminating null character), and the contents of the array are indeterminate.<br />

strcoll() has better performance with respect to strxfrm() in cases where a given <strong>string</strong> is compared<br />

to other <strong>string</strong>s only a few times, or where the <strong>string</strong>s to be compared are long but a difference in<br />

2 Hewlett-Packard Company − 2 − <strong>HP</strong>-UX 11i Version 3: September 2010


<strong>string</strong>(<strong>3C</strong>)<br />

<strong>string</strong>(<strong>3C</strong>)<br />

the <strong>string</strong>s that determines their relative ordering usually comes among the first few characters.<br />

strxfrm() offers better performance in, for example, a sorting routine where a number of <strong>string</strong>s are<br />

each transformed just once and the transformed versions are compared against each other many times.<br />

EXTERNAL INFLUENCES<br />

Locale<br />

The LC_CTYPE category determines the interpretation of the bytes within the <strong>string</strong> arguments to the<br />

strcoll() and strxfrm() functions as single and/or multibyte characters. It also determines the<br />

case conversions to be done for the strcasecmp() and strncasecmp() functions.<br />

The LC_COLLATE category determines the collation ordering used by the strcoll() and strxfrm()<br />

functions.<br />

International Code Set Support<br />

Single- and multibyte character code sets are supported for the strcoll() and strxfrm() functions.<br />

Only single-byte character code sets are supported for the strcasecmp() and strncasecmp() functions.<br />

EXAMPLES<br />

The following sample piece of code finds the tokens, separated by blanks, that are in the <strong>string</strong> s (assuming<br />

that there are at most MAXTOK tokens):<br />

int i = 0;<br />

char *s, *last, *tok[MAXTOK];<br />

tok[0] = strtok_r(s, " ", &last);<br />

while (tok[++i] = strtok_r(NULL, " ", &last));<br />

WARNINGS<br />

The functions strcat(), strncat(), strcpy(), strncpy(), strtok(), and strtok_r() alter<br />

the contents of the array to which s1 points. They do not check for overflow of the array.<br />

Null pointers for destination <strong>string</strong>s cause undefined behavior.<br />

Character movement is performed differently in different implementations, so moves involving overlapping<br />

source and destination <strong>string</strong>s may yield surprises.<br />

The transformed <strong>string</strong> produced by strxfrm() for a language using an 8-bit code set is usually at<br />

least twice as large as the original <strong>string</strong> and may be as much four times as large (ordinary characters<br />

occupy two bytes each in the transformed <strong>string</strong>, 1-to-2 characters four bytes, 2-to-1 characters two bytes<br />

per original pair, and don’t-care characters no bytes). Each character of a multibyte code set (Asian<br />

languages) occupies three bytes in the transformed <strong>string</strong>.<br />

For functions strcoll() and strxfrm() results are undefined if the languages specified by the<br />

LC_COLLATE and LC_CTYPE categories use different code sets.<br />

Users of strtok_r() should also note that the prototype of this function will change in the next release<br />

for conformance with the new POSIX Threads standard.<br />

s<br />

AUTHOR<br />

<strong>string</strong>() was developed by the University of California, Berkeley, AT&T, OSF, and <strong>HP</strong>.<br />

SEE ALSO<br />

conv(<strong>3C</strong>), malloc(<strong>3C</strong>), memory(<strong>3C</strong>), setlocale(<strong>3C</strong>), thread_safety(5), glossary(9).<br />

STANDARDS CONFORMANCE<br />

strcat(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strchr(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strcmp(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strcoll(): AES, SVID3, XPG3, XPG4, ANSI C<br />

strcpy(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strcspn(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strdup(): SVID2, SVID3<br />

<strong>HP</strong>-UX 11i Version 3: September 2010 − 3 − Hewlett-Packard Company 3


<strong>string</strong>(<strong>3C</strong>)<br />

<strong>string</strong>(<strong>3C</strong>)<br />

strlen(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strncat(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strncmp(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strncpy(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strpbrk(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strrchr(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strspn(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strstr(): AES, SVID3, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strtok(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C<br />

strxfrm(): AES, SVID3, XPG3, XPG4, ANSI C<br />

s<br />

4 Hewlett-Packard Company − 4 − <strong>HP</strong>-UX 11i Version 3: September 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!