- bcmp :比较两个内存中的内容
- bcopy : 复制内存中的内容
- bzero : 将一个内存内容全清零
- ffs : 在一个整数中查找第一个值为真的位
- index : 查找字符串中第一个出现的指定字符
- memccpy :复制内存中的内容
- memchr :在一块内存指定范围内查找一个指定字符
- memcmp :比较内存中存放的内容
- memcpy : 复制一块内存内容到另一块
- memfrob : 对某个内存区重新编码
- memmove : 复制内存内容
- memset :将某值填入到一个内存区域
- rindex :查找字符串中最后一个出现的指定字符
- strcasecmp :忽略大小写比较字符串
- strcat :将一个字符串连接到另一个字符串的尾部
- strchr ;查找字符串中指定字符
- strcmp :比较两个字符串
- strcoll :根据当前环境信息来比较字符串
- strcpy :复制一个字符串的内容到另一个字符串中
- strdup :复制字符串的内容
- strfry :随机重组一个字符串
- strlen : 返回字符串的长度
- strncasecmp :忽略大小写比较两个字符串
- strncat :将一个字符串的前n个字符连接到另一个字符串的尾部
- strncmp :比较字符串前n个字符
- strncpy : 复制字符串的前n个字符
- strpbrk :查找字符串中第一个出现的指定字符
- strrchr :查找字符串中最后一个出现的指定字符
- strspn :计算字符串中由指定字符集字符组成的子字符串的长度
- strcspn :计算字符串中由非指定字符集字符组成的子字符串的长度
bcmp :比较两个内存中的内容
#include<stdio.h> #include<string.h> int main() { char s1[] = "abca"; char s2[] = "abcb"; char a = 'a'; int b = 97; printf("%d\n",bcmp(s1,s2,3)); printf("%d\n",bcmp(s1,s2,4)); printf("%d\n",bcmp(s1,s2,5)); printf("%d\n",bcmp(&a,&b,1)); return 0; }//运行结果:0,-1,-1,0 /* 函数原型:int bcmp(const void *s1,const void *s2,int n) 函数的功能是比较前n个字节是否相同,相同返回0,否则返回非零值 */
bcopy : 复制内存中的内容
#include<stdio.h> #include<string.h> int main() { char s1[10] = "0"; char s2[] = "hello"; bcopy(s2,s1,5); printf("%s\n",s1); return 0; } //运行结果 hello /* 函数原型void bcopy(const void* src,const void* dest,int n) 函数功能是将src的前n个字节拷贝到dest中 */
bzero : 将一个内存内容全清零
#include<stdio.h> #include<string.h> int main() { char s[] = "hello world"; bzero(s,5); printf("[%s]\n",s); printf("[%s]\n",s+5); int a[] = {1,2,3,4,5,6,7}; bzero(a,5); int i; for (i=0;i<7;i++) { printf("[%d]",a[i]); } return 0; } /* 运行结果: [] [ world] [0][0][3][4][5][6][7],一个int型是4个字节 函数原型:bzero(void *s,int n),将s指向的内容后n个字节清零 */
ffs : 在一个整数中查找第一个值为真的位
#include<stdio.h> #include<string.h> int main() { int a[] = {0,1,2,3,4,5}; int pos = -1; int i; for (i=0;i<6;i++) { pos = ffs(a[i]); printf("the true bit of number %d is : %d\n",a[i],pos); } return 0; } /* 运行结果: the true bit of number 0 is : 0 the true bit of number 1 is : 1 //二进制1 the true bit of number 2 is : 2 //二进制10 the true bit of number 3 is : 1 //二进制11 the true bit of number 4 is : 3 //二进制100 the true bit of number 5 is : 1 //二进制101 函数原型:int ffs(int i) 函数返回第一个为1的位置,若找到为1 的位置,返回1到31之间的值,否则返回0 */
index : 查找字符串中第一个出现的指定字符
#include<stdio.h> #include<string.h> int main() { char s[] = "hello world"; char *ptr; printf("[%s]\n",index(s,'o')); printf("[%s]\n",index(s,'l')); return 0; } /* 运行结果: [o world] [llo world] 函数原型:char *index(const char *s,int c) 如果找到参数c,则返回指向该字符的指针,否则返回NULL */
memccpy :复制内存中的内容
#include<stdio.h> #include<string.h> int main() { char s1[20] = "0"; char s2[] = "welcome to china"; char *p; p = (char*)memccpy(s1,s2,'o',strlen(s2)); printf("%s\n",s1); return 0; } /* 运行结果:welco 函数原型:void* memccpy(void *dest,const void*src,int c,size_t n); 如果c代表的字符在源内存区中存在,返回指向下一个字符的指针,否则返回NULL */
memchr :在一块内存指定范围内查找一个指定字符
#include<stdio.h> #include<string.h> int main() { char s[] = "welcome to china"; char *p; p = (char*) memchr(s,'t',10); printf("%s\n",p); return 0; } /* 运行结果:to china 函数原型 void memchr(const void*s,int c,size_t n) 在指定的内存区域的前n个字节中查找c,如果找到参数c,返回指向c的指针,否则返回null */
memcmp :比较内存中存放的内容
#include<stdio.h> #include<string.h> int main() { char s1[] = "hello linux"; char s2[] = "hello world"; printf("%d\n",memcmp(s1,s2,5)); printf("%d\n",memcmp(s1,s2,8)); return 0; } /* 运行结果:0,-1 函数原型 int memcmp(const void*s1,cosnt void*s2,size_t n) 比较两个内存区前n个字节的大小 */
memcpy : 复制一块内存内容到另一块
#include<stdio.h> #include<string.h> int main() { char s2[] = "welcome to china"; char s1[] = "0"; char *p = (char*)memcpy(s1,s2,10); printf("%d\n",(long)(&s1)-(long)(&s2)); printf("%s\n",p); printf("%s\n",s1); printf("%s\n",s2); return 0; } /* 运行结果: -2 welcome toto china welcome toto china lcome toto china 函数原型void* memcpy(void *dest,const void*src,size_t n),返回指向dest的指针 注意:当参数dest和src有重叠时,结果不可预测,可能出现错误 */
memfrob : 对某个内存区重新编码
#include<stdio.h> #include<string.h> int main() { char s[] = "123456"; memfrob(s,8); printf("<%s>\n",s); memfrob(s,8); printf("<%s>\n",s); return 0; } /* 运行结果 <乱码> <123456> 函数原型 void *memfrob(void*s,size_t n) 对指定内存区的前n个字节进行编码 */
memmove : 复制内存内容
void
*
memmove
(
void
*
dest, const
void
*
src, size_t
n)
功能和memcpy一样,只是当内存区域出现重叠时,memmove依然可以正常执行复制
memmove的处理措施:
(1)当源内存的首地址等于目标内存的首地址时,不进行任何拷贝
(2)当源内存的首地址大于目标内存的首地址时,实行正向拷贝
(3)当源内存的首地址小于目标内存的首地址时,实行反向拷贝
memset :将某值填入到一个内存区域
#include<stdio.h> #include<string.h> int main() { char s[] = "hello world"; char *p = memset(s,'a',5); printf("%s\n",p); printf("%s\n",s); return 0; } /* 运行结果: aaaaa world aaaaa world 函数原型 void memset(void *dest,int c,size_t n) 将参数c填入参数dest所指向的内存区的前n个字节中 */
rindex :查找字符串中最后一个出现的指定字符
#include<stdio.h> #include<string.h> int main() { char s[] = "hello world"; char *p = rindex(s,'o'); printf("%s\n",p); printf("%s\n",s); return 0; } /* 运行结果: ord hello world 函数原型 char* rindex(const char*s,int c) 反向查找指定字符,返回指向该字符的指针,否则返回null */
strcasecmp :忽略大小写比较字符串
同strcmp,只不过忽略了大小写
strcat :将一个字符串连接到另一个字符串的尾部
#include<stdio.h> #include<string.h> int main() { char s[20] = "hello world"; char *p = strcat(s," hello"); printf("%s\n",p); printf("%s\n",s); return 0; } /* 运行结果: hello world hello hello world hello 函数原型 char *strcat(char* dest,char* src) 注意总的字符串之和不能超过dest的长度 */
strchr ;查找字符串中指定字符
#include<stdio.h> #include<string.h> int main() { char s[20] = "hello world"; char *p = strchr(s,'o'); printf("%s\n",p); return 0; } /* 运行结果: o world 函数原型 char * strchr(const char*s,int c) 找到指定字符,返回指向该字符的指针,否则返回null */
strcmp :比较两个字符串
同memcmp,只不过memcmp比较的是内存内容,而strcmp比较的是字符串
strcoll :根据当前环境信息来比较字符串
根据LC_COLLATE目录来进行比较
strcpy :复制一个字符串的内容到另一个字符串中
函数原型:char* strcpy(char*dest,char*src),拷贝字符串
strdup :复制字符串的内容
#include<stdio.h> #include<string.h> int main() { char s[] = "hello world"; char *p = strdup(s); printf("%s\n",p); return 0; } /* 运行结果: hello world 函数原型char *strdup(const char* str) 使用malloc分配内存空间,返回复制后的地址 */
strfry :随机重组一个字符串
#include<stdio.h> #include<string.h> int main() { char s[] = "hello"; printf("%s\n",strfry(s)); printf("%s\n",strfry(s)); printf("%s\n",strfry(s)); return 0; } /* 运行结果: ohlel eohll lhoel 函数原型char *strfry(char *s) 随机重组字符串,返回首地址 */
strlen : 返回字符串的长度
函数原型:size_t strlen(const char*s),返回'\0'之前的字符数
strncasecmp :忽略大小写比较两个字符串
函数原型 :int strncasecmp(const char*s1,const char*s2,size_t int)
同strcmp,只不过忽略大小
strncat :将一个字符串的前n个字符连接到另一个字符串的尾部
函数原型: char* strncat(char *dest,const char* src,size_t n)
strncmp :比较字符串前n个字符
函数原型:int strncmp(const char*s1,const char*s2,size_t n),比较前n个字符的大小
strncpy : 复制字符串的前n个字符
函数原型 :char* strncpy(char*dest,const char*src,size_t n),
复制前n个字符,如果碰到'\0',复制停止
strpbrk :查找字符串中第一个出现指定字符集中指定的字符
#include<stdio.h> #include<string.h> int main() { char s[] = "hello"; char charset[] = "abcde"; printf("%s\n",strpbrk(s,charset)); return 0; } /* 运行结果: ello 函数原型:char* strpbrk(const char*s,const char*accept) */
strrchr :查找字符串中最后一个出现的指定字符
同strchr,反向查找
函数原型:char* strrchr(const char*s,int c)
strspn :计算字符串中由非指定字符集字符组成的子字符串的长度
函数原型:size_t strscpn(const char*s,cosnt char*accept)
strcspn :计算字符串中由非指定字符集字符组成的子字符串的长度
函数原型:size_t strcscpn(const char*s,cosnt char*reject)