一些常用的字符串处理函数总结memcpy、memmove、memset、memcmp、memchr等

(1)原型void *memcpy(void *dest, const void *src, size_t n); 

字符串复制函数,从内存区域src拷贝n个字节到dest,必须确保src和dest是内存不重复的区域。

函数返回指向dest的指针

eg:#include
        #include
        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;
}

运行结果:the old b is cpy.
    the old a is abcdef.
    the new b is abcde .

(2)原型void *memmove(void *dest, const void *src, size_t n);

复制n个字节的内存区域从src到dest,src和dest的内存区域可以重叠,因为函数拷贝时是先将src放到临时数组中,然后再从临时数组中取到dest中。

函数返回一个指向dest的指针。

#include
#include


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);//这里重叠了会报错
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指向内容为:%s.\n",p);
printf("the new a is %s .\n",a);
return 0;
}

运行结果:

the old b is cpy cpyliu.
the new b is p cpyliiuu . 该处内存重叠了,得到了错误的结果。
the old a is cpy cpyliu.

p指向内容为:py cpyliuu.
the new a is py cpyliuu .

(3)函数原型void *memset(void *s, int c, size_t n);

用常量c来填充s所指向区域的前那个字节。

用memset来初始化内存空间。

#include
#include

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;
}

运行结果:

the old a is liu
the new a is 

(4)函数原型int memcmp(const void *s1, const void *s2, size_t n);

比较s1和s2所指向的前那个字节,当两字节的前n个中s1>s2,返回一个正数;s1=s2返回0;s1

#include
#include

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;
}

运行结果:

the compare result is 0
the compare result is 104
the compare result is -104

(5)函数原型:void *memchr(const void *s, int c, size_t n); 在s指向的前n个内存区域找c是否存在

#include
#include
#include

int main(void)
{   
char *a="beckham";
char *p1=NULL;
        char *p2=NULL;

p1=memchr(a,'c',3);//找到后返回该字符处的指针
p2=memchr(a,'c',2);//未找到返回原来的指针


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

return 0;
}

运行结果:

the result is ckham
the result is (null)

(6)函数原型char *strcpy(char *dest, const char *src);

从源src拷贝字符知道遇到‘\0’结束到dest,dest应该足够大。函数返回指向dest的指针

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

拷贝src中的前n个字符到dest。

例子:

#include
#include

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;
}

运行结果:

before copy,tha b is  .
before copy,tha c is  .
the result of strcpy is beckham
the result of strncpy is bec

(7)函数原型:

char *strcat(char *dest, const char *src);
将src指向的字符串连接在dest字符串尾部,dest必须有足够的空间去存储。

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

将src指向的字符串前n个字符串连接在dest字符串尾部,dest必须有足够的空间去存

#include
#include

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;
}

运行结果:

the result of strcat is who is beckham
the result of strncat is who is be

(8)int strcmp(const char *s1, const char *s2);
    返回值和memcmp近似,但是strcmp会在\0停下来。
    int strncmp(const char *s1, const char *s2, size_t n);

    只比较钱n个字符。两个函数的返回值都是整形

#include
#include

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;
}

运行结果:

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)函数原型:char *strdup(const char *s);

                    字符串复制,返回指向被复制的字符串头指针。
       char *strndup(const char *s, size_t n); 

      字符串自己复制前n字节,返回指向被复制的字符串头指针。

#include
#include

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;
}

运行结果:

the result of strdup is abc
the result of strndup is ab

(10)函数原型:char *strchr(const char *s, int c);

函数返回c首次出现在s中的指针.

#include
#include

int main(void)
{   

char *b="abcdef";
char *c=NULL;
a=strchr(b);
printf("the result of strchr is %s\n",a);
return 0;
}

运行结果:the result of strchr is cdef

(11)函数原型:char *strtok(char *str, const char *delim);

str为要分解的字符串,delim为分隔符字符串。第一次调用strtok函数时,需要指明str参数,在以后调用strtok函数时,该参数必须为NULL。

#include
#include
int main(void)
{   
char b[]={"abc def liu"};//如果定义char *b="abc def liu",则str指向只读区域不能修改。字符串是只读的,尝试修改会造成段错误
                                       //而函数原型中是会修改的
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;
}

运行结果:

the result of  is abc
the result of  is def
the result of  is liu





























你可能感兴趣的:(一些常用的字符串处理函数总结memcpy、memmove、memset、memcmp、memchr等)