一些常用的字符串处理函数总结memcpy、memmove、memset、memcmp、memchr等
来源:互联网 发布:对数据进行标准化处理 编辑:程序博客网 时间:2024/05/26 09:54
(1)原型void *memcpy(void *dest, const void *src, size_t n);
字符串复制函数,从内存区域src拷贝n个字节到dest,必须确保src和dest是内存不重复的区域。
函数返回指向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;
}
运行结果: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<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);//这里重叠了会报错
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<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;
}
运行结果:
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<s2返回一个负数。
#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;
}
运行结果:
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<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);//找到后返回该字符处的指针
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<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;
}
运行结果:
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<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;
}
运行结果:
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<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;
}
运行结果:
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<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;
}
运行结果:
the result of strdup is abc
the result of strndup is ab
(10)函数原型:char *strchr(const char *s, int c);
函数返回c首次出现在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;
}
运行结果:the result of strchr is cdef
(11)函数原型:char *strtok(char *str, const char *delim);
str为要分解的字符串,delim为分隔符字符串。第一次调用strtok函数时,需要指明str参数,在以后调用strtok函数时,该参数必须为NULL。
#include<stdio.h>
#include<string.h>
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等
- 内存操作函数memcpy,memccpy,memmove,memchr,memcmp,memicmp,memset
- 内存操作函数memcpy,memccpy,memmove,memchr,memcmp,memicmp,memset
- 部分字符串库函数的重写(源码) toupper,memcpy,memmove,memset,memchr,strlen,strcpy,strcat,strcmp,strchr,
- 部分字符串库函数的重写(源码) toupper,memcpy,memmove,memset,memchr,strlen,strcpy,strcat,strcmp,strchr
- 部分字符串库函数的重写(源码) toupper,memcpy,memmove,memset,memchr,strlen,strcpy,strcat,strcmp,strchr
- 部分字符串库函数的重写(源码) toupper,memcpy,memmove,memset,memchr,strlen,strcpy,strcat,strcmp,strchr
- memcmp,memcpy,memmove,memset,strcat,strcmp,strcpy,strlen 8个函数的实现
- 自己实现的库函数(memset,memcmp,memcpy,memmove)
- memset,memcpy,memcmp的原型函数
- memcpy,strcpy,memset,memmove的函数实现
- memset()、memcpy()、memcmp()的使用方法
- c语言中字符串函数strlen()、strcat()、strcpy()、strcmp()及内存操作函数memset()、memcpy()、memcmp()、memmove()的使用及编码实现
- C和指针之字符串memcpy、memmove、memset使用总结
- 字符串函数的原型实现(strcpy, strcat, strcmp, memset, memcpy, memmove)
- C操作内存函数(memset,memcpy,memcmp)
- 内存拷贝函数strcpy()、memcpy()、memmove()、memset()的用法
- strcpy、memset、memmove、memcpy和memccpy函数的使用
- 统计难题 (HDU_1251) 字典树
- 面对选择,优先自己的兴趣
- hadoop集群配置之hive1.2.0安装部署(远程mysql)
- struts2学习笔记--------struts2处理流程
- 陈波20150402
- 一些常用的字符串处理函数总结memcpy、memmove、memset、memcmp、memchr等
- yii 命令行模式
- 设计模式之适配器模式(Adapter)
- PAT-A1015. Reversible Primes (20)
- 第二周项目2——就拿胖子说事
- 数码管实验
- TYVJ 1117 BFS
- Java Web页面修饰技巧——CSS应用
- struts2学习笔记--------动态方法调用和使用通配符定义action