STRING(3C) COMMAND REFERENCE STRING(3C) NAME string, strcat, strncat, strcatn, strcmp, strncmp, strcmpn, strcpy, strncpy, strcpyn, strlen, index, strchr, rindex, strrchr, strpbrk, strspn, strcspn, strtok, strtrn, strntrn - string operations SYNOPSIS #include <strings.h> char *strcat(s1, s2) char *s1, *s2; char *strncat(s1, s2, n) char *s1, *s2; int n; char *strcatn(s1, s2, n) char *s1, *s2; int n; strcmp(s1, s2) char *s1, *s2; strncmp(s1, s2, n) char *s1, *s2; int n; strcmpn(s1, s2, n) char *s1, *s2; int n; char *strcpy(s1, s2) char *s1, *s2; char *strncpy(s1, s2, n) char *s1, *s2; int n; char *strcpyn(s1, s2, n) char *s1, *s2; int n; int strlen(s) char *s; char *index(s, c) char *s, c; char *strchr(s, c) char *s, c; Printed 3/13/89 1
STRING(3C) COMMAND REFERENCE STRING(3C) char *rindex(s, c) char *s, c; char *strrchr(s, c) char *s, c; char *strpbrk(s1, s2) char *s1, *s2; int strspn(s1, s2) char *s1, *s2; int strcspn(s1, s2) char *s1, *s2; char *strtok(s1, s2) char *s1, *s2; char *strtrn(s, from, to) char *s, *from, *to; char *strntrn(s, n, from, to) char *s, *from, *to; int n; DESCRIPTION These functions operate on null-terminated strings. They do not check for overflow of any receiving string. Strcat appends a copy of string s2 to the end of string s1. Strncat and strcatn copy at most n characters. Both return a pointer to the null-terminated result. Strcmp compares its arguments and returns an integer greater than, equal to, or less than 0, depending on if s1 is lexicographically greater than, equal to, or less than s2. Strncmp and strcmpn make the same comparison but looks at at most n characters. Strcpy copies string s2 to s1, stopping after the null character has been moved. Strncpy and strcpyn copy exactly n characters, truncating or null-padding s2; the target may not be null-terminated if the length of s2 is n or more. All three return s1. Strlen returns the number of nonnull characters in s. Index (rindex) returns a pointer to the first (last) occurrence of character c in string s, or 0 if c does not occur in the string. The null character terminating the string is considered to be part of the string. Printed 3/13/89 2
STRING(3C) COMMAND REFERENCE STRING(3C) Strchr is a synonym for index. Strrchr is a synonym for rindex. Strpbrk returns a pointer to the first occurrence in string s1 of any character from string s2, or NULL if no character from s2 exists in s1. Strspn (strcspn) returns the length of the initial segment of string s1 which consists entirely of characters from ( not from ) string s2. Strtok considers the string s1 to consist of a sequence of zero or more text tokens separated by spans of one or more from the separator string s2. The first call (with pointer s1 specified) returns a pointer to the first character of the first token, and will have written a NULL character into s1 immediately following the returned token. If there are no separators in the string, the entire string is returned. As long as the string pointed at by s1 remains unchanged, subsequent calls with 0 for the first argument will work through string s1 in this way until no tokens remain. The separator string s2 may be different from call to call. When no token remains in s1, a NULL is returned. Strtrn and strntrn implement the function of the utility tr(1) in a subroutine. The string given is translated using a given key. The string s is a pointer to the string to be translated. The integer n is the maximum number of characters to be translated in the string. From and to are pointers to strings containing the translation key. Each character position in each string has a corresponding character in the other. If the key strings are not of equal length, the shorter is padded using the last character in the string (not including the null terminator). As with tr(1), the key strings may contain range. The range a-e is expanded as abcde. A reversed range, like e-a is not expanded, but is interpreted as the three characters e,-, and a. If the from string pointer is NULL, the last set of key strings given are used again. In this way, a translation can be repeated a number of times without having the key strings expanded each time. EXAMPLES For the following examples, constant strings are used, but pointers to strings work the same way. The following example uses strpbrk: strpbrk ("abcde", "ce") The pointer returned points to the string cde. Printed 3/13/89 3
STRING(3C) COMMAND REFERENCE STRING(3C) This example uses strspn: strspn ("abcde", "cda") The number returned is 1. The following example uses strcpy and strtok: char *s, *t; (void) strcpy (s, "field1:field2 field3"); t = strtok (s, ": "); printf ("%s ", t); t = strtok (0, ": "); printf ("%s ", t); t = strtok (0, ": d"); printf ("%s\n", t); This sequence of calls, when executed, cause the text: field1 field2 field3 to be displayed on the standard output. The following example uses strncpy and strtrn. In this example, each uppercase character in each string is converted to lowercase: char *s, *t; (void) strncpy (s, "First STRING."); t = strtrn (s, "A-Z", "a-z"); printf ("%s\n", t); t = strtrn ("Second STRING.", 0); printf ("%s\n", t); When executed, the above code would display the following two lines on the standard output : first string. second string. CAVEATS Note that comparison of characters outside of the seven-bit ASCII character set (for example, those with octal values greater than 0177) may be unpredictable except that all such characters will be considered either all greater than or all less than the standard ASCII character set. All string movement is performed character by character starting at the left (the first character in the string). Thus, overlapping moves toward the left will work as expected, but overlapping moves to the right may yield surprises. Printed 3/13/89 4
STRING(3C) COMMAND REFERENCE STRING(3C) Since the character NULL is the string terminator, the translation routines are not able to translate null characters. The call: strtrn (string, chars, "") produces undefined results and should not be used. SEE ALSO bstring(3c) and printf(3s). Printed 3/13/89 5
%%index%% na:312,262; sy:574,4635;5593,2953; de:8546,2093;11023,3190; ex:14213,404;15001,1379; ca:16380,646;17410,287; se:17697,188; %%index%%000000000156