[C standard library] & lt; string. h & gt;, standard library string. h

Source: Internet
Author: User
Tags strtok types of functions

[C standard library] <string. h>, standard library string. h

Reference link: C standard library-<string. h>

There are two main types of functions in string. h:

Memxxx and strxxx, in which memxxx is a function for memory operations,It does not stop when '\ 0' is encountered.Generally, a parameter of the size_t type (actually unsigned int) is set to indicate the byte size;

Strxxx is a function for string operations,When '\ 0' is reached. In strxxx functions, some functions are strnxxx. These functions can be passed in a size_t type parameter to indicate the byte size.When '\ 0' or byte size is reached, it will stop., Relatively safe.

 

The following grouping introduces functions:

1. memcpy memmove strcpy strncpy

void *memcpy(void *dest, const void *src, size_t n);void *memmove(void *dest, const void *src, size_t n);char *strcpy(char *dest, const char *src);char *strncpy(char *dest, const char *src, size_t n);
 
// Memory copy. When copying a string, you can copy it to str1. At this time, if str2 has \ 0, the copy operation will continue until it reaches n times memcpy (str1, str2, sizeof (str1)-1); str1 [sizeof (str1) -1] = '\ 0'; // memmove is safer in case of memory overlap and will not cause overwriting. // http://stackoverflow.com/questions/4415910/memcpy-vs-memmovechar str5 [] = "aabbcc "; printf ("The string: % s \ n", str5); memcpy (str5, str5 + 2, 4); // cccccccc, wrongprs INTF ("New string: % s \ n ", str5); strncpy (str5," aabbcc ", sizeof (str5); // reset stringprintf (" T He string: % s \ n ", str5); memmove (str5, str5 + 2, 4); // bbcccc, rightprintf (" New string: % s \ n ", str5); // copy a string. The same is true for strncpy. However, memcpy does not consider the problem of '\ 0' in the middle, while strncpy stops copying ret = strncpy (str1, str2, sizeof (str1)-1) When \ 0 is encountered ); str1 [sizeof (str1)-1] = '\ 0'; // if the space of str1 is insufficient to put down str2, it will cause memory overflow ret = strcpy (str1, str2 );

  

2. memcmp strcmp strncmp

Stackoverflow provides an example of the answer: what-is-the-difference-between-memcmp-strcmp-and-strncmp-in-c

Strcmp compares strings ending with '\ 0'.

Strncmp compares strings ending with '\ 0' with a maximum of n characters.

Memcmp compares the binary buffer of n Bytes.

void *memcpy(void *dest, const void *src, size_t n);char *strcpy(char *dest, const char *src);char *strncpy(char *dest, const char *src, size_t n);

 

    const char s1[] = "atoms\0\0\0\0";  // extra null bytes at end    const char s2[] = "atoms\0abc";     // embedded null byte    const char s3[] = "atomsaaa";    if(strcmp(s1, s2) == 0){printf("strcmp(s1, s2) == 0 \n");}      // strcmp stops at null terminator    if(strcmp(s1, s3) != 0){printf("strcmp(s1, s3) != 0 \n");}      // Strings are different    if(strncmp(s1, s3, 5) == 0){printf("strncmp(s1, s3, 5) == 0 \n");}  // First 5 characters of strings are the same    if(memcmp(s1, s3, 5) == 0){printf("memcmp(s1, s3, 5) == 0 \n");}   // First 5 bytes are the same    if(strncmp(s1, s2, 8) == 0){printf("strncmp(s1, s2, 8) == 0 \n");}  // Strings are the same up through the null terminator    if(memcmp(s1, s2, 8) != 0){printf("memcmp(s1, s2, 8) != 0 \n");}   // First 8 bytes are different

  

3. memchr strchr strrchr

In memory, memchr returns the pointer of the first matched character after starting from an address to n Bytes.

Strchr returns the pointer of the first matched character in a string.

Strrchr returns the pointer of the last matched character in a string.

void *memchr(const void *str, int c, size_t n);char *strchr(const char *str, int c);char *strrchr(const char *str, int c);

  

Char chr [] = "there is an orange"; const char * memchrres = memchr (chr, 'R', 12 ); // The int type parameter in the middle must be a char character .. Const char * strchrres = strchr (chr, 'R'); const char * strrchrres = strrchr (chr, 'R'); printf ("memchrres :( % p) % s, strchrres :( % p) % s, strrchrres :( % p) % s \ n ", & memchrres, memchrres, & strchrres, strchrres, & strrchrres, strrchrres );

  

4. memset: set all the content of the first n bytes in the memory to the ASCII value specified by ch. The first value is the specified memory address, the block size is specified by the third parameter. This function initializes the newly applied memory and returns a pointer to s (from Baidu encyclopedia)

void *memset(void *str, int c, size_t n);

  

struct S abc;memset(&abc, 0, sizeof(struct S));

 

5. strstr: In the stringHaystackSearch for the first occurrence of a stringNeedle(Does not contain null ending characters.

char *strstr(const char *haystack, const char *needle);

  

    const char haystack[20] = "W3CSchool lalala";    const char needle[10] = "School";    char *strres;    strres = strstr(haystack, needle);    printf("%s \n", strres);

  

6. strlen strnlen

Strlen calculates the length of the string until the end character is null, but does not contain the end character.

Strnlen and above functions are insecure. If the string is invalid (it does not contain '\ 0'), you must specify the maximum matching length to prevent memory overflow.

size_t strlen(const char *str);size_t strnlen(const char *str, size_t maxlen);

  

Const char strlenres [] = "test strlen"; printf ("strlen = % d \ n", (int) strlen (strlenres )); // If the string does not have \ 0, the system determines that the error printf ("strnlen = % d \ n", (int) strnlen (strlenres, sizeof (strlenres) is incorrect )));

  

7. strcat strncat

Strcat appends the source string to the end of the target string

Strncat specifies the maximum number of appends n, which is relatively safe.

char *strcat(char *dest, const char *src);char *strncat(char *dest, const char *src, size_t n);

  

    char cat1[20] = "lalala";    char cat2[] = "short";    char cat3[] = "longlong";    printf("res1 = %s \n", strcat(cat1, cat2));    printf("res2 = %s \n", strncat(cat1, cat2, sizeof(cat1) - 1 - strlen(cat1)));    printf("res3 = %s \n", strncat(cat1, cat3, sizeof(cat1) - 1 - strlen(cat1)));

  

8. strtok: Splits a long string based on the given separator (the method is weird ..)

char *strtok(char *str, const char *delim);

  

Char tok [80] = "This is-www. w3cschool. cc-website "; const char delim [] ="-"; char * token = strtok (tok, delim); // get the first string while (token! = NULL) {printf ("% s \ n", token); token = strtok (NULL, delim); // note! }

  

 

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.