Memory and String functions of "Linux C Chinese function manual"

Source: Internet
Author: User
Tags define function strcmp strtok

Memory and String functions 1) bcmp Compare memory contents

Correlation function bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
Table header file #include <string.h>
Defines the function int bcmp (const void *s1,const void * s2,int N);
The function Description bcmp () is used to compare the first n bytes of the memory interval referred to by S1 and S2, or 0 if the parameter n is 0.
Return value returns a value of 0 if the contents of the memory referred to by the parameter S1 and S2 are identical, otherwise a value other than 0 is returned.
Additional instructions are recommended to replace with MEMCMP ().

2) bcopy Copy memory contents

Correlation function memccpy,memcpy,memmove,strcpy,ctrncpy
Table header file #include <string.h>
Defines the function void bcopy (const void *src,void *dest, int n);
Function Description bcopy () and memcpy () are used to copy the first n bytes of the memory content referred to by SRC
To the address referred to by dest, but the parameter src is opposite to the dest when it is passed to the function.
return value
Additional instructions suggest using memcpy () instead

3) bzero the memory content to zero

Correlation function Memset,swab
Table header file #include <string.h>
Defines the function void bzero (void *s,int n);
The function Description Bzero () sets the first n bytes of the memory area that the parameter S refers to, all to the zero value. Equivalent to calling memset ((void*) s,0,size_tn);
return value
Additional instructions suggest using memset instead

4) index finds the first occurrence of the specified character in a string

Correlation function RINDEX,SRECHR,STRRCHR
Table header file #include <string.h>
Defines the function char * index (const char *s, int c);
The function Description index () is used to find the first occurrence of the parameter C address in the parameter S string, and then returns the address where the character appears.
The string ending character (NULL) is also considered part of the string.
The return value returns the address of the character if the specified character is found, otherwise 0 is returned.

5) memccpy Copy memory contents

Correlation function bcopy,memcpy,memmove,strcpy,strncpy
Table header file #include <string.h>
Defines the function void * memccpy (void *dest, const void * src, int c,size_tn);
The function Description memccpy () is used to copy the first n bytes of the memory content referred to by SRC to the address referred to by dest. Unlike the memcpy (),
memccpy () checks if the parameter C appears at copy time, or returns the next byte address in dest with the value C.
The return value returns a pointer to the next byte in Dest that has a value of C. A return value of 0 indicates that there are no bytes of value C in the first n bytes of the memory referred to in SRC.

6) MEMCHR find a specific character in a range of memory

Correlation function Index,rindex,strchr,strpbrk,strrchr,strsep,strspn,strstr
Table header file #include <string.h>
Defines the function void * MEMCHR (const void *s,int c,size_t N);
Function Description MEMCHR () Searches from scratch for the first n bytes of the memory content referred to by S, until it finds the first byte with a value of C, and returns a pointer to that byte.
The return value returns a pointer to the byte if the specified byte is found, otherwise 0 is returned.

7) memcmp Compare memory contents

Correlation function bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp
Table header file #include <string.h>
Defines the function int memcmp (const void *s1,const void *s2,size_t n);
The function Description memcmp () is used to compare the first n characters of the memory interval referred to by S1 and S2. The comparison of the string size is determined in the order of the ASCII Code table, and the secondary order is also the value of the character.
MEMCMP () First subtracts the first character value of the S1 from the value of the first character of S2, and then continues to compare the next character if the difference is 0, and returns the difference if the difference is not 0. For example
The string "Ac" and "ba" comparisons return the difference between the characters ' A ' (65) and ' B ' (98) (-33).
Return value returns a value of 0 if the contents of the memory referred to in Parameters S1 and S2 are identical. A value greater than 0 is returned if the S1 is greater than S2. A value less than 0 is returned if the S1 is less than S2.

8) memcpy Copy memory contents

Correlation function bcopy,memccpy,memcpy,memmove,strcpy,strncpy
Table header file #include <string.h>
Defines the function void * memcpy (void * dest, const void *SRC, size_t n);
The function Description memcpy () is used to copy the first n bytes of the memory content referred to by SRC to the memory address referred to by dest. Unlike strcpy (), memcpy () will be complete
Copy n bytes, and will not end because the string ends with ' \ '.
The return value returns a pointer to Dest.
Additional instruction Pointers src and dest do not overlap the memory areas referred to.

9) memmove Copy memory contents

Correlation function bcopy,memccpy,memcpy,strcpy,strncpy
Table header file #include <string.h>
Defines the function void * Memmove (void *dest,const void *src,size_t n);
Function Description Memmove () and memcpy () are used to copy the first n bytes of the memory content referred to by SRC to the address referred to by dest.
The difference is that when the memory area referred to by SRC and dest is overlapping, memmove () can still handle it correctly, but the execution efficiency is slightly slower than using memcpy ().
The return value returns a pointer to Dest.
Additional instruction pointers Src and dest refer to memory areas that can overlap.

Memset A memory space into a value

Correlation function Bzero,swab
Table header file #include <string.h>
Defines the function void * memset (void *s, int c, size_t N);
The function Description memset () fills in the first n bytes of the memory area referred to by the parameter S as parameter C, and then returns a pointer to S. When writing a program,
Memset () is quite handy if you need to initialize an array.
The return value returns a pointer to S.
The additional description parameter C, although declared as int, must be unsigned char, so the range is between 0 and 255.

One) Rindex find the last occurrence of the specified character in a string

Correlation function INDEX,MEMCHR,STRCHR,STRRCHR
Table header file #include <string.h>
Defines the function char * rindex (const char *s,int c);
The function Description Rindex () is used to find the last parameter C address in the parameter S string, and then returns the address where the character appears.
The string ending character (NULL) is also considered part of the string.
The return value returns the address where the character is located if the specified character is found, otherwise 0 is returned.

strcasecmp Ignore Case comparison string

Correlation function bcmp,memcmp,strcmp,strcoll,strncmp
Table header file #include <string.h>
Defines the function int strcasecmp (const char *S1, const char *S2);
The function Description strcasecmp () is used to compare the arguments S1 and S2 strings, and the case differences are automatically ignored when compared.
The return value returns 0 if the argument S1 and the S2 string are the same. S1 length greater than s2 length returns a value greater than 0, and a value less than 0 is returned if the S1 length is less than S2 length.

Strcat Connect two strings

Correlation function bcopy,memccpy,memcpy,strcpy,strncpy
Table header file #include <string.h>
Defines the function char *strcat (char *dest,const char *src);
The function Description strcat () copies the parameter src string to the end of the string referred to by the parameter dest. The first parameter dest to have enough space to hold the string to be copied.
Return value returns the string start address of the parameter dest

STRCHR find the first occurrence of the specified character in a string

Correlation function Index,memchr,rinex,strbrk,strsep,strspn,strstr,strtok
Table header file #include <string.h>
Defines the function char * STRCHR (const char *s,int c);
The function Description STRCHR () is used to find the first parameter C address in the parameter S string, and then returns the address where the character appears.
The return value returns the address of the character if the specified character is found, otherwise 0 is returned.

strcmp comparison string

Correlation function Bcmp,memcmp,strcasecmp,strncasecmp,strcoll
Table header file #include <string.h>
Defines the function int strcmp (const char *s1,const char *S2);
The function Description strcmp () is used to compare parameters S1 and S2 strings. The comparison of the string size is determined in the order of the ASCII code table, which is also the value of the character.
strcmp () First S1 the first character value minus S2 First character value, if the difference is 0 then continue to compare the next character, if the difference is not 0 will be the difference
Return. For example, the string "Ac" and "ba" comparisons return the difference between the characters "A" (65) and ' B ' (98) (-33).
The return value returns 0 if the argument S1 and the S2 string are the same. A value greater than 0 is returned if the S1 is greater than S2.
A value less than 0 is returned if the S1 is less than S2.

Strcoll compare strings using the current region's character sequence

Correlation function strcmp,bcmp,memcmp,strcasecmp,strncasecmp
Table header file #include <string.h>
Defines the function int strcoll (const char *S1, const char *S2);
The function Description Strcoll () compares S1 and S2 strings according to the order of text specified by the environment variable lc_collate.
The return value returns 0 if the argument S1 and the S2 string are the same. A value greater than 0 is returned if the S1 is greater than S2. A value less than 0 is returned if the S1 is less than S2.
Additional instructions if lc_collate is "POSIX" or "C", then Strcoll () works exactly like strcmp ().

strcpy Copy String

Correlation function Bcopy,memcpy,memccpy,memmove
Table header file #include <string.h>
Defines the function char *strcpy (char *dest,const char *src);
The function Description strcpy () copies the parameter SRC string to the address referred to by the parameter dest.
The return value returns the string start address of the parameter dest.
Additional instructions if the memory space referred to by the parameter dest is not large enough, it may cause buffering
Overflow (buffer Overflow) error condition, please pay special attention when writing the program, or use strncpy () to replace.

STRCSPN returns the number of characters in a string that do not contain the specified string contents consecutively

Correlation function strspn
Table header file #inclued <string.h>
Defines the function size_t strcspn (const char *s,const char * reject);
The function Description strcspn () computes consecutive characters from the beginning of the argument s string, which are not in the string that the parameter reject refers to. Simply put, if STRCSPN () returns a value of n, it means that the string s starts with n characters and does not contain characters within the string reject.
The return value returns the number of characters in the string reject that are not contained in string S.
Example:

#include <string.h>
void Main ()
{
Char *str= "Linux is first developed for 386/486-based PCs.";
printf ("%d\n", strcspn (str, ""));
printf ("%d\n", strcspn (str, "/-"));
printf ("%d\n", strcspn (str, "1234567890"));
}
Execution Result:
5/* Only the "" appears, so return the length of "Linux" */
33/* Calculated to the "/" or "-", so return to the length of "6" */
30/* Evaluates to a number character, so returns the length of "3" before it appears.

StrDup) Copy String

Correlation function Calloc,malloc,realloc,free
Table header file #include <string.h>
Defines the function char * strdup (const char *s);
The function Description StrDup () uses Maolloc () to configure the same amount of space as the parameter S string, and then copies the contents of the parameter S string to the memory address.
Then return the address. The address can finally be freed using free ().
The return value returns a string pointer to the new string address after the copy. If NULL is returned, it indicates insufficient memory.

strlen return string length

Related functions
Table header file #include <string.h>
Defines the function size_t strlen (const char *s);
The function Description strlen () is used to calculate the length of the specified string s, excluding the end character "\".
The return value returns the number of characters in the string s.

strncasecmp Ignore Case comparison string

Correlation function bcmp,memcmp,strcmp,strcoll,strncmp
Table header file #include <string.h>
Defines the function int strncasecmp (const char *s1,const char *s2,size_t N);
The function Description strncasecmp () is used to compare the first n characters of the arguments S1 and S2 strings, and the case differences are automatically ignored when compared.
The return value returns 0 if the argument S1 and the S2 string are the same. A value greater than 0 is returned if the S1 is greater than S2, and a value less than 0 is returned if the S1 is less than S2.

Strncat Connect two strings

Correlation function bcopy,memccpy,memecpy,strcpy,strncpy
Table header file #inclue <string.h>
Defines the function char * strncat (char *dest,const char *src,size_t N);
The function Description Strncat () copies the parameter SRC string n characters to the end of the string referred to by the parameter dest. The first parameter dest to have enough space
To accommodate the string to be copied.
The return value returns the string start address of the parameter dest.

) strncpy Copy String

Correlation function Bcopy,memccpy,memcpy,memmove
Table header file #include <string.h>
Defines the function char * strncpy (char *dest,const char *src,size_t N);
The function Description strncpy () copies the argument SRC string to the first n characters to the address referred to by the parameter dest.
The return value returns the string start address of the parameter dest.

Strpbrk finds the first occurrence of a specified character in a string

Correlation function Index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
Table header file #include <include.h>
Defines a function char *strpbrk (const char *s,const char *accept);
The function Description strpbrk () is used to locate any character in the argument s string that first appears in the parameter accept string.
The return value returns the address of the character if the specified character is found, otherwise 0 is returned.

STRRCHR find the last occurrence of a specified character in a string

Correlation function Index,memchr,rindex,strpbrk,strsep,strspn,strstr,strtok
Table header file #include <string.h>
Defines the function char * STRRCHR (const char *s, int c);
The function Description STRRCHR () is used to find the last parameter C address in the parameter S string, and then returns the address where the character appears.
The return value returns the address of the character if the specified character is found, otherwise 0 is returned.

STRSPN) returns the number of characters in a string that do not contain the specified string contents consecutively

Correlation function Strcspn,strchr,strpbrk,strsep,strstr
Table header file #include <string.h>
Defines the function size_t strspn (const char *s,const char *accept);
The function Description strspn () computes successive characters from the beginning of the argument s string, which are exactly the characters in the string that the accept refers to.
Simply put, if STRSPN () returns a value of n, it means that the string s starts with n characters that are all characters in the string accept.
The return value returns the number of characters in a string that begins consecutively with the strings.
Example:

#include <string.h>
void Main ()
{
Char *str= "Linux is first developed for 386/486-based
PCs. ";
Char
*t1= "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf ("%d\n", Strspn (STR,T1));
}
Execution Result:
5/* Calculates uppercase and lowercase letters. Does not contain "", so returns the length of Linux. */

STRSTR) to find the specified string in a string

Correlation function Index,memchr,rindex,strchr,strpbrk,strsep,strspn,strtok
Table header file #include <string.h>
Defines a function char *strstr (const char *haystack,const char *needle);
The function Description Strstr () will search for the string needle from the string haystack and return the address that appears for the first time.
The return value returns the address of the first occurrence of the specified string, or 0.
Example:

#include <string.h>
void Main ()
{
char * s= "012345678901234567890123456789";
Char *p;
P= Strstr (S, "901");
printf ("%s\n", p);
printf ("%p\n", &p);
}
Execution Result:
901234567890123456789
0xbfdbd2f8

Strtok) Split string

Correlation function Index,memchr,rindex,strpbrk,strsep,strspn,strstr
Table header file #include <string.h>
define function char * strtok (char *s,const char *delim);
The function Description Strtok () is used to split a string into fragments. The parameter S points to the string to be split, the parameter Delim is the split string,
When Strtok () finds the split character of the parameter Delim in the string of the parameter s, the character is changed to the \ s character.
At the first call, Strtok () must give the parameter S string, and the subsequent call sets the parameter s to null.
Each successful call returns the next segmented string pointer.
The return value returns the next segmented string pointer and returns NULL if it is no longer divisible.
Example:

#include <string.h>
void Main ()
{
Char s[]= "Ab-cd:ef;gh:i-jkl;mnop;qrs-tu:vwx-y;z";
Char *delim= "-:";
Char *p;
printf ("%s", Strtok (S,delim));
while ((P=strtok (Null,delim))) printf ("%s", p);
printf ("\ n");
}
Execution Result:
AB CD ef;gh i jkl;mnop;qrs tu vwx y;z/* '-' and ': ' characters have been replaced by '/' characters */

Memory and String functions of "Linux C Chinese function manual"

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.