Common string processing (cforlinux)

Source: Internet
Author: User
Tags strtok
Common string processing (cforlinux)-General Linux technology-Linux programming and kernel information. For details, refer to the following section. Prototype: extern int bcmp (const void * s1, const void * s2, int n );
Usage: # include
Function: Compares the first n Bytes of the string s1 and s2.
NOTE: If s1 = s2 or n = 0, zero is returned; otherwise, a non-zero value is returned. Bcmp does not check NULL. (S1> s2 return> 0; s1 Example: char s1 [5] = "abcde"; char s2 [5] = "abcDe"; s1 [2] = '\ 0 '; s2 [2] = '\ 0'; bcmp (s1, s2, 5 );

Prototype: extern void bcopy (const void * src, void * dest, int n );
Usage: # include
Function: Copies the first n Bytes of the string src to the dest (src, dest does not have to be a string pointer)
Note: bcopy does not check NULL bytes in the string. The function does not return values.
Example: struct {int a; char s [5];} t1, t2; bcopy (& t1, & t2, sizeof (t2 ));

Prototype: extern void bzero (void * s, int n );
Usage: # include
Function: set the first n Bytes of the byte string s to zero (s does not have to be a string pointer)
Note: bzero has no return value.
Example: struct {int a; char s [5];} tt; bzero (& tt, sizeof (tt ));

Prototype: extern void * memccpy (void * dest, void * src, unsigned char ch, unsigned int count );
Usage: # include
Function: Copies a maximum of count bytes from the memory region indicated by src to the memory region indicated by dest. If the ch character is encountered, the replication is stopped.
Note: return the pointer pointing to the first character after the ch character (ch is also copied to dest, pointing to dest). If ch does not exist in the First n Bytes of src, return NULL.
Example: char s [5] = "abcde"; char d [5], * p = NULL; s [1] = '\ 0'; p = memccpy (d, s, 'd, 5 );

Prototype: extern void * memchr (void * buf, char ch, unsigned count );
Usage: # include
Function: searches for the character ch from the first count bytes of the memory area specified by buf.
Note: When the first ch character is encountered, stop searching. If the call succeeds, a pointer pointing to the ch character is returned; otherwise, NULL is returned.
Example: char s [5] = "abcde"; char * p = NULL; s [1] = '\ 0'; p = memchr (s, 'C ', 5 );

Prototype: extern int memcmp (void * buf1, void * buf2, unsigned int count );
Usage: # include
Function: Compares the first count bytes of buf1 and buf2 in the memory area.
Note: When buf1 When buf2 is used, the return value is greater than 0.
Example: char s1 [6] = "Hello! "; Char s2 [6] =" Hello! "; * (S1 + 1) = '\ 0'; * (s2 + 1) =' \ 0'; r = memcmp (s1, s2, 6 );

Prototype: extern void * memcpy (void * dest, void * src, unsigned int count );
Usage: # include
Function: copy count bytes from the memory area indicated by src to the memory area indicated by dest.
Note: the memory areas specified by src and dest cannot overlap (in case of overlap, an exception occurs when src is in front of dest after the address). The function returns a pointer to dest.
Example: char s [26] = "abcdefghijklmnopqrstuvwxyz"; s [3] = 0; char * p = NULL; p = memcpy (s, s + 2, 24 );
Char s [26] = "abcdefghijklmnopqrstuvwxyz"; s [3] = 0; char * p = NULL; p = memcpy (s + 2, s, 24 );///

Prototype: extern void * memmove (void * dest, const void * src, unsigned int count );
Extern void * movmem (void * src, void * dest, unsigned int count); (for windows)
Usage: # include
Function: copy count bytes from the memory area indicated by src to the memory area indicated by dest.
Note: src and dest indicate that the memory areas can overlap, but the src content will be changed after replication. The function returns the pointer to the dest.
Example: char s [26] = "abcdefghijklmnopqrstuvwxyz"; s [3] = 0; char * p = NULL; p = memmove (s + 2, s, 24 );

Prototype: extern void * memset (void * buffer, int c, int count );
Extern void * setmem (void * buf, unsigned int count, char ch); (for windows)
Usage: # include
Function: sets the first count byte of the memory area referred to by buffer to character c.
Note: The pointer to the buffer is returned.
Example: char s [] = "asdfasssdfg"; memset (s, 'G', 6); // If char * s is used, the segment is incorrect.

Prototype: extern char * stpcpy (char * dest, char * src );
Usage: # include
Function: Copies the src string ending with NULL to the array indicated by dest.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings. Returns the pointer pointing to the end of the dest character (NULL ).
Example: char s [26] = "abcdefghijklmnopqrstuvwx"; char d [26] = {0}; char * p = NULL; p = stpcpy (d, s );

Prototype: extern char * strcat (char * dest, char * src );
Extern char * strncat (char * dest, char * src, int n); (the n characters of src are followed by dest)
Usage: # include
Function: add the src string to the end of the dest (overwrite '\ 0' at the end of the dest) and add' \ 0'
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings. Returns the pointer to dest.
Example:

Prototype: extern char * strchr (char * s, char c );
Usage: # include
Function: locate the first occurrence of character c in string s.
Returns the pointer to the position where c appears for the first time. If c does not exist in s, NULL is returned.
Example:

Prototype: extern int strcmp (char * s1, char * s2 );
Extern int strncmp (char * s1, char * s2, int n); (Compare n bytes)
Extern int stricmp (char * s1, char * s2); extern int strcmpi (char * s1, char * s2); (case insensitive for windows)
Extern int strnicmp (char * s1, char * s2, int n); extern int strncmpi (char * s1, char * s2, int n); (Compare n bytes)
Extern int strcasecmp (const char * s1, const char * s2); (case-insensitive for linux)
Extern int strncasecmp (const char * s1, const char * s2, size_t n); (case insensitive, n Bytes compared for linux)
Usage: # include
Function: Compares s1 and s2 strings.
Note: When s1 Return value> 0 in s2
Example:

Prototype: extern char * strcpy (char * dest, char * src );
Extern char * strncpy (char * dest, char * src, int n); (src Length Usage: # include
Function: Copies the src string ending with NULL to the array indicated by dest.
Note: The memory areas specified by src and dest cannot overlap and dest must have sufficient space to accommodate src strings. Returns the pointer to dest.
Example:

Prototype: extern int strcspn (char * s1, char * s2 );
Extern int strspn (char * s1, char * s2); position of any character other than s2 that appears for the first time in s1 (positions start from 0)
Extern char * strpbrk (char * s1, char * s2 ())
Usage: # include
Function: Search for characters in s2 in string s1.
Description: Position of the first occurrence of any character in s2 (starting from 0)
Returns the substring length of the first character that appears in s1, that is, the substring that does not appear in S1.
Example: char * s = "Golden wlobal View"; char * r = "new"; int n = 0; n = strcspn (s, r); (n = 4)

Prototype: extern char * strdup (char * s );
Usage: # include
Function: Copy string s
Returns the pointer to the copied string. the required space is automatically allocated by malloc () and can be automatically released by free ().
Example: char * s = "Golden Global View"; char * d; d = strdup (s );

Prototype: extern int strlen (char * s );
Usage: # include
Function: calculates the length of string s.
Description: returns the length of s, excluding the terminator NULL.
Example:

Prototype: int toupper (int c );
Int tolower (int c );
Extern char * strlwr (char * s); (converts string s to uppercase/lowercase for windows)
Extern char * strupr (char * s );
Usage: # include
Function: converts uppercase letters.
Note:
Example:

Prototype: extern char * strstr (char * haystack, char * needle );
Usage: # include
Function: Find the first position of the needle from the string haystack (not compare the terminator NULL ).
Note: The pointer pointing to the needle position for the first time is returned. If no pointer is found, NULL is returned.
Example: char * s = "Golden Global View"; char * l = "lob"; char * p; p = strstr (s, l );

Prototype: extern char * strtok (char * s, char * delim );
Usage: # include
Function: Splits a string into a group of tag strings. S is the string to be decomposed, and delim is the separator string
NOTE: For the first call, s must point to the string to be decomposed, and then the call should set s to NULL.
Strtok searches for characters contained in delim in s and replaces them with NULL ('\ 0') until the entire string is searched.
Returns the string pointing to the next tag. If no string is marked, NULL is returned.
Example: char s [26] = "Goldenn GlobanaView"; char * d = "n"; char * p = NULL; p = strtok (s, d ); p = strtok (NULL, d );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.