Some commonly used string processing functions summarize memcpy, Memmove, Memset, memcmp, MEMCHR, etc.

Source: Internet
Author: User
Tags function prototype printf strcmp strlen strtok

(1) prototype void *memcpy (void *dest, const void *SRC, size_t n);

The string copy function, from the memory area src to copy n bytes to dest, must ensure that SRC and dest are areas of memory that are not duplicated.

function returns a pointer to Dest

Eg: #include <stdio.h>
#include <string.h>
int main (void)
{
Char *a= "abcdef";
Char b[20]= "cpy";

printf ("The old B is%s.\n", b);
memcpy (b,a,5);

printf ("The old A is%s.\n", a);

printf ("The new B is%s. \ n", b);
return 0;
}

Running result: the old B is cpy.
The old A is abcdef.
The new B is ABCDE.

(2) prototype void *memmove (void *dest, const void *SRC, size_t n);

Memory regions that replicate N bytes of memory can overlap from src to dest,src and dest, because the function copies the src into a temporary array and then takes it from the temporary array to the dest.

The function returns a pointer to Dest.

#include <stdio.h>
#include <string.h>


int main (void)
{
Char *p=null;
Char b[]= "cpy Cpyliu";
Char a[]= "cpy Cpyliu";

printf ("The old B is%s.\n", b);
memcpy (B,b+1,strlen (b)-1);//The overlap here will be an error.
printf ("The new B is%s. \ n", b);

printf ("The old A is%s.\n", a);
P=memmove (A,a+1,strlen (a)-1);
printf ("p points to content:%s.\n", p);
printf ("The new A is%s. \ n", a);
return 0;
}

Operation Result:

The old B is cpy Cpyliu.
The new B is P Cpyliiuu. The memory in the place overlaps and the result is incorrect.
The old A is cpy Cpyliu.

P points to content: Py Cpyliuu.
The new is a py cpyliuu.

(3) function prototype void *memset (void *s, int c, size_t N);

Use the constant c to fill the previous byte of the area to which s is pointing.

Use Memset to initialize the memory space.

#include <stdio.h>
#include <string.h>

int main (void)
{

Char a[20]= "Liu";
printf ("The old A is%s\n", a);
memset (A,0,sizeof (a));
printf ("The New A is%s\n", a);

return 0;
}

Operation Result:

The old A is Liu
The new A is

(4) function prototype int memcmp (const void *S1, const void *S2, size_t n);

Compares the previous byte pointed to by S1 and S2, returns a positive number when two bytes of the first n medium s1>s2, S1=S2 returns 0;S1<S2 returns a negative number.

#include <stdio.h>
#include <string.h>

int main (void)
{
int c,d,e;
Char a[20]= "Beckham";
Char b[20]= "Beck";


C=MEMCMP (a,b,3);
D=MEMCMP (a,b,5);
E=MEMCMP (b,a,5);
printf ("The Compare result is%d\n", c);
printf ("The Compare result is%d\n", d);
printf ("The Compare result is%d\n", e);

return 0;
}

Operation Result:

The compare result is 0
The Compare result is 104
The Compare result is-104

(5) Function prototype: void *MEMCHR (const void *s, int c, size_t N); Find if c exists in the first n memory areas of S point

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void)
{
Char *a= "Beckham";
Char *p1=null;
Char *p2=null;

P1=MEMCHR (A, ' C ', 3);//Returns the pointer at the character after it is found
P2=MEMCHR (A, ' C ', 2);//not found return the original pointer


printf ("The result is%s\n", p1);
printf ("The result is%s\n", p2);

return 0;
}

Operation Result:

The result is Ckham
The result is (null)

(6) Function prototype char *strcpy (char *dest, const char *SRC);

Copy characters from the source Src know that it should be large enough to meet the end of ' dest,dest '. function returns a pointer to Dest

Char *strncpy (char *dest, const char *SRC, size_t N);

Copies the first n characters in Src to dest.

Example:

#include <stdio.h>
#include <string.h>

int main (void)
{
Char *a= "Beckham";
Char b[20]={0};
Char c[20]={0};

printf ("Before Copy,tha B is%s. \ n", b);
printf ("Before Copy,tha c is%s. \ n", c);
printf ("The result of strcpy is%s\n", strcpy (B,a));
printf ("The result of strncpy is%s\n", strncpy (c,a,3));

return 0;
}

Operation Result:

Before Copy,tha B is.
Before Copy,tha C is.
The result of strcpy is Beckham
The result of strncpy is Bec

(7) Function prototype:

Char *strcat (char *dest, const char *SRC);
The string that the SRC points to is concatenated at the tail of the dest string, and dest must have enough space to store.

Char *strncat (char *dest, const char *SRC, size_t N);

The first n strings of the string that the SRC points to are concatenated to the tail of the dest string, and dest must have enough space to save

#include <stdio.h>
#include <string.h>

int main (void)
{
Char *a= "Beckham";
Char b[30]={"who is"};
Char c[30]={"who is"};


printf ("The result of Strcat is%s\n", strcat (B,a));
printf ("The result of Strncat is%s\n", Strncat (c,a,3));

return 0;
}

Operation Result:

The result of Strcat is Beckham
The result of Strncat is a is

(8) int strcmp (const char *S1, const char *S2);
The return value and the memcmp approximation, but strcmp will stop at.
int strncmp (const char *S1, const char *S2, size_t N);

Compare only the sum of the money n characters. The return values of the two functions are all shaped

#include <stdio.h>
#include <string.h>

int main (void)
{
Char *a= "Beckh";
Char b[30]={"Beck\0"};

printf ("The result of strcmp between A and B is%d\n", strncmp (b,a,4));
printf ("The result of strncmp between A and B is%d\n", strncmp (b,a,3));
printf ("The result of strcmp between A and B is%d\n", memcmp (b,a,5));

return 0;
}

Operation Result:

The result of strcmp between A and B is 0
The result of strncmp between A and B is 0
The result of strcmp between A and B is-104

(9) Function prototype: char *strdup (const char *s);

A string copy that returns a pointer to the string header being copied.
Char *strndup (const char *s, size_t N);

The string copies the first n bytes itself, returning a pointer to the copied string header.

#include <stdio.h>
#include <string.h>

int main (void)
{

Char *b= "ABC";
Char *a={""};
Char *c={""};
A=strdup (b);
C=strndup (b,2);
printf ("The result of StrDup is%s\n", a);
printf ("The result of Strndup is%s\n", c);
return 0;
}

Operation Result:

The result of StrDup is ABC
The result of Strndup is AB

(10) Function prototype: char *STRCHR (const char *s, int c);

The function returns a pointer to the first occurrence of C in S.

#include <stdio.h>
#include <string.h>

int main (void)
{

Char *b= "abcdef";
Char *c=null;
A=STRCHR (b);
printf ("The result of STRCHR is%s\n", a);
return 0;
}

Operating result: The result of STRCHR is Cdef

(11) Function prototype: Char *strtok (char *str, const char *delim);

STR is the string to be decomposed, Delim is the delimiter string. The first time you call the Strtok function, you need to indicate the STR parameter, which must be null at a later call to the Strtok function.

#include <stdio.h>
#include <string.h>
int main (void)
{
Char b[]={"ABC def Liu"};//If you define char *b= "ABC def Liu", str points to a read-only region that cannot be modified. The string is read-only and attempting to modify will cause a fragment error
And in the function prototype, it's going to change.
Char *r= "";
Char *c=strtok (B,R);
printf ("The result of is%s\n", c);
C=strtok (NULL,R);
printf ("The result of is%s\n", c);
C=strtok (NULL,R);
printf ("The result of is%s\n", c);
return 0;
}

Operation Result:

The result of IS ABC
The result of is Def
The result of IS Liu





























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.