String Handling: string.h

I l @ ve RuBoard

String Handling: string.h

The string.h library defines the size_t type and the NULL macro for the null pointer. It provides several functions for analyzing and manipulating character strings and a few that deal with memory more generally . Table F.16 lists the functions.

Table  F.16. Localization functions.
Prototype Description
void * memchr (const void *s, int c, size_t n); Searches for the first occurrence of c (converted to unsigned char) in the initial n characters of the object pointed to by s ; returns a pointer to the first occurrence, NULL if none is found.
int memcmp (const void *s1, const void *s2, size_t n); Compares the first n characters of the object pointed to by s1 to the first n characters of the object pointed to by s2 , interpreting each value as unsigned char ; the two objects are identical if all n pairs match; otherwise , the objects compare as the first unmatching pair; returns zero if the objects are the same, less than zero if the first object is numerically less than the second, and greater than zero if the first object is greater.
void * memcpy (void *s1, const void *s2, size_t n); Copies n bytes from the location pointed to by s2 to the location pointed to by s1 ; behavior is undefined if the two locations overlap; returns the value of s1 .
void * memmove (void *s1, const void *s2, size_t n); Copies n bytes from the location pointed to by s2 to the location pointed to by s1 ; behaves as if copying; first uses a temporary location so that copying to an overlapping location works; returns the value of s1 .
void * memset (void *s, int v, size_t n); Copies the value v (converted to type unsigned char ) to the first n bytes pointed to by s ; returns s .
char *strcat(char *s1, const char *s2); Appends a copy of the string pointed to by s2 (including the null character) to the location pointed to by s1 ; the first character of the s2 string overwrites the null character of the s1 string; returns s1 .
char * strncat (char *s1, const char *s2, size_t n); Appends a copy up to n characters or up to the null character from the string pointed to by s2 to the location pointed to by s1 , with the first character of s2 overwriting the null character of s1 ; a null character is always appended; the function returns s1 .
char * strcpy (char *s1, const char *s2); Copies the string pointed to by s2 (including the null character) to the location pointed to by s1 ; returns s1 .
char * strncpy (char *s1, const char *s2, size_t n); Copies up to n characters or up to the null character from the string pointed to by s2 to the location pointed to by s1 ; if the null character in s2 occurs before n characters are copied , null characters are appended to bring the total to n ; if n characters are copied before reaching a null character, no null character is appended; the function returns s1 .
int strcmp(const char *s1, const char *s2); Compares the strings pointed to by s1 and s2 ; two strings are identical if all pairs match; otherwise, the strings compare as the first unmatching pair; characters are compared using the character code values; the function returns zero if the strings are the same, less than zero if the first string is less than the second, and greater than zero if the string array is greater.
int strcoll (const char *s1, const char *s2); Works like strcmp() except that it uses the collating sequence specified by the LC_COLLATE category of the current locale as set by the setlocale() function.
int strncmp (const char *s1, const char *s2, size_t n); Compares up to the first n characters or up to the first null character of the arrays pointed to by s1 and s2 ; two arrays are identical if all tested pairs match; otherwise, the arrays compare as the first unmatching pair; characters are compared using the character code values; the function returns zero if the arrays are the same, less than zero if the first array is less than the second, and greater than zero if the first array is greater.
size_t strxfrm (char *s1, const char *s2, size_t n); Transforms the string in s2 and copies up to n characters, including a terminating null character, to the array pointed to by s1 ; the criterion for the transformation is that two transformed strings will be placed in the same order by strcmp() as strcoll() would place the untransformed strings; the function returns the length of the transformed string (not including the terminal null character).
char * strchr (const char *s, int c); Searches for the first occurrence of c (converted to char ) in the string pointed to by s ; the null character is part of the string; returns a pointer to the first occurrence, or NULL if none is found.
size_t strcspn (const char *s1, const char *s2); Returns the length of the maximum initial segment of s1 that does not contain any of the characters found in s2 .
char * strpbrk (const char *s1, const char *s2); Returns a pointer to th elocation of the first character in s1 to match any of the characters in s2 ; returns NULL if no match is found.
char * strrchr (const char *s, int c); Searches for the last occurrence of c (converted to char) in the string pointed to by s ; the null character is part of the string; returns a pointer to the first occurrence, or NULL if none is found.
size_t strspn (const char *s1, const char *s2); Returns the length of the maximum initial segment of s1 that consists entirely of characters from s2 .
char * strstr (const char *s1, const char *s2); Returns a pointer to the location of the first occurrence in s1 of the sequence of characters in s2 (excluding the terminating null character); returns NULL if no match is found.
char * strtok (char *s1, const char *s2); This function decomposes the string s1 into separate tokens; the string s2 contains the characters that are recognized as token separators. The function is called sequentially. For the initial call, s1 should point to the string to be separated into tokens. The function locates the first token separator that follows a non-separator character and replaces it with a null character. It returns a pointer to a string holding the first token. If no tokens are found, it returns NULL . To find further tokens in the string, call strtok() again, but with NULL as the first argument. Each subsequent call returns a pointer to the next token or to NULL if no further tokens are found. See the example following this table.
char * strerror(int errnum); Returns a pointer to an implementation-dependent error message string corresponding to the error number stored in errnum .
int strlen(const char * s); Returns the number of characters (excluding the terminating null character) in the string s .

The strtok() function is a bit unusual in how it is used, so here is a short example:

 #include <stdio.h> #include <string.h> int main(void) {     char data[] = "  C is\t  too#much\nfun!";     const char tokseps[] = " \t\n#"; /* separators  */     char * pt;     puts(data);     pt = strtok(data,tokseps);       /* intial call  */     while (pt)                       /* quit on NULL */     {         puts (pt);                   /* show token   */         pt = strtok(NULL, tokseps);  /* next token   */     }     return 0; } 

Here is the output:

 C is    too#much fun! C is too much fun! 
I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net