Summary of common string manipulation functions in C language

Source: Internet
Author: User

1. bcmp

Prototype: extern int bcmp (const void *S1, const void *s2, int n);

Usage: #include <string.h>

Function: Compares the first n bytes of a string S1 and S2 for equality

Description: Returns 0 for equality, otherwise returns a value other than 0

2. Bcopy

Prototype: extern void bcopy (const void *SRC, const void *dest, int n);

Usage: #include <string.h>

Function: Copy the first n bytes of the string src into the dest

Description: bcopy does not check null bytes in the string null, the function does not return a value

3. Bzero

Prototype: extern void bzero (void *s, int n);

Usage: #include <string.h>

Function: Place the first n bytes of the string s to 0

Description: No return value

4. memcpy

Prototype: extern void *memcpy (void *dest, void* src, unsigned int count);

Usage: #include <string.h>

Function: Copy count bytes from the memory area referred to by SRC to the area of memory referred to by dest

Description: The memory area referred to by SRC and dest cannot overlap, and the function returns a pointer to Dest

5. memccpy

Prototype: extern void *memccpy (void *dest, void* src, unsigned char ch, unsigned int count);

Usage: #include <string.h>

Function: the memory area referred to by SRC is not more than the count byte to the memory area referred to by dest, and if the character Ch is encountered, stop copying

Description: The return value points to the character ch after the first character of the pointer, if the SRC first n bytes do not exist in CH then return null,ch is copied

6. MEMCHR

Prototype: extern void *memchr (void *buf, char ch, unsigned int count);

Usage: #include <string.h>

Function: Find characters from the first count byte of the memory area referred to by BUF CH

Description: Stops searching when a character ch is encountered. If successful, returns a pointer to the character CH; otherwise returns null

7. memcmp

Prototype: extern void *memcmp (void *buf1, void *buf1, unsigned int count);

Usage: #include <string.h>

Function: Compare the previous count bytes of the memory area BUF1 and BUF2

Description: When Buf1<buf2, the return value <0

8. Memmove

Prototype: extern void *memmove (void *dest, const void *SRC, unsigned int count);

Usage: #include <string.h>

Function: Copy count bytes from the memory area referred to by SRC to the area of memory referred to by dest

Description: The memory areas referred to by SRC and dest can overlap, but the SRC content is changed after copying, and the function returns a pointer to Dest

1#include <stdio.h>2#include <string.h>3 intMainvoid)4 {5     Char* s ="Hello World";6Memmove (S, s+3, strlen (s)-3);7S[strlen (s)-3]=0;8printf"s:%s\n", s);9 Ten     return 0; One } A /*output: S:lo World*/
Memmove Example9. Memset

Prototype: extern void *memset (void *buffer, int c, int count);

Usage: #include <string.h>

Function: Sets the first count byte of the memory area indicated by buffer to character C

Description: Returns a pointer to buffer

Ten. Movmem

Prototype: extern void *movmem (void *src, void* dest, unsigned int count);

Usage: #include <string.h>

Function: Copy count bytes from the memory area referred to by SRC to the area of memory referred to by dest

Description: The memory areas referred to by SRC and dest can overlap, but the SRC content is changed after copying, and the function returns a pointer to Dest

stpcpy.

Prototype: extern char *stpcpy (char* dest, char* src);

Usage: #include <string.h>

function: Copy the null-terminated string from SRC to the array referred to by dest

Description: The memory areas referred to by SRC and dest cannot overlap and dest must have enough controls to accommodate the SRC string, returning a pointer to the character at the end of the Dest (NULL)

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.