C语言字符串函数速查word版.docx
《C语言字符串函数速查word版.docx》由会员分享,可在线阅读,更多相关《C语言字符串函数速查word版.docx(39页珍藏版)》请在冰豆网上搜索。
C语言字符串函数速查word版
字符串函数
1)bcmp
2)bcopy
3)bzero
4)memccpy
5)memchr
6)memcmp
7)memcpy
8)memicmp
9)memmove
10)memset
11)movmem
12)setmem
13)stpcpy
14)strcat
15)strchr
16)strcmp
17)strcmpi
18)strcpy
19)strcspn
20)strdup
21)stricmp
22)strlen
23)strlwr
24)strncat
25)strncmp
26)strncmpi
27)strncpy
28)strnicmp
29)strpbrk
30)strrev
31)strset
32)strstr
33)strtok
34)strupr
bcmp
原型:
externintbcmp(constvoid*s1,constvoid*s2,intn);
用法:
#include
功能:
比较字符串s1和s2的前n个字节是否相等
说明:
如果s1=s2或n=0则返回零,否则返回非零值。
bcmp不检查NULL。
举例:
//bcmp.c
#include
#include
main()
{
char*s1="GoldenGlobalView";
char*s2="Goldenglobalview";;
clrscr();//clearscreen
if(!
bcmp(s1,s2,7))
printf("s1equaltos2infirst7bytes");
else
printf("s1notequaltos2infirst7bytes");
getchar();
clrscr();
if(!
bcmp(s1,s2,12))
printf("s1equaltos2infirst12bytes");
else
printf("s1notequaltos2infirst12bytes");
getchar();
return0;
}
bcopy
原型:
externvoidbcopy(constvoid*src,void*dest,intn);
用法:
#include
功能:
将字符串src的前n个字节复制到dest中
说明:
bcopy不检查字符串中的空字节NULL,函数没有返回值。
举例:
//bcopy.c
#include
#include
main()
{
char*s="GoldenGlobalView";
chard[20];
clrscr();//clearscreen
bcopy(s,d,6);
printf("s:
%s\n",s);
printf("d:
%s\n",d);
getchar();
clrscr();
s[13]=0;
bcopy(s+7,d,11);//bcopyignorenullinstring
printf("%s\n",s+7);
for(i=0;i<11;i++)
putchar(d[i]);
getchar();
return0;
}
bzero
原型:
externvoidbzero(void*s,intn);
用法:
#include
功能:
置字节字符串s的前n个字节为零。
说明:
bzero无返回值。
举例:
//bzero.c
#include
#include
main()
{
struct
{
inta;
chars[5];
floatf;
}tt;
chars[20];
bzero(&tt,sizeof(tt));//structinitializationtozero
bzero(s,20);
clrscr();
printf("InitailSuccess");
getchar();
return0;
}
memccpy
原型:
externvoid*memccpy(void*dest,void*src,unsignedcharch,unsignedintcount);
用法:
#include
功能:
由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。
说明:
返回指向字符ch后的第一个字符的指针,如果src前n个字节中不存在ch则返回NULL。
ch被复制。
举例:
//memccpy.c
#include
#include
main()
{
char*s="GoldenGlobalView";
chard[20],*p;
clrscr();
p=memccpy(d,s,'x',strlen(s));
if(p)
{
*p='\0';//MUSTDoThis
printf("Charfound:
%s.\n",d);
}
else
printf("Charnotfound.\n");
getchar();
return0;
}
memchr
原型:
externvoid*memchr(void*buf,charch,unsignedcount);
用法:
#include
功能:
从buf所指内存区域的前count个字节查找字符ch。
说明:
当第一次遇到字符ch时停止查找。
如果成功,返回指向字符ch的指针;否则返回NULL。
举例:
//memchr.c
#include
#include
main()
{
char*s="Hello,Programmers!
";
char*p;
clrscr();
p=memchr(s,'P',strlen(s));
if(p)
printf("%s",p);
else
printf("NotFound!
");
getchar();
return0;
}
memcmp
原型:
externintmemcmp(void*buf1,void*buf2,unsignedintcount);
用法:
#include
功能:
比较内存区域buf1和buf2的前count个字节。
说明:
当buf1当buf1=buf2时,返回值=0
当buf1>buf2时,返回值>0
举例:
//memcmp.c
#include
#include
main()
{
char*s1="Hello,Programmers!
";
char*s2="Hello,programmers!
";
intr;
clrscr();
r=memcmp(s1,s2,strlen(s1));
if(!
r)
printf("s1ands2areidentical");
else
if(r<0)
printf("s1lessthans2");
else
printf("s1greaterthans2");
getchar();
return0;
}
memcpy
原型:
externvoid*memcpy(void*dest,void*src,unsignedintcount);
用法:
#include
功能:
由src所指内存区域复制count个字节到dest所指内存区域。
说明:
src和dest所指内存区域不能重叠,函数返回指向dest的指针。
举例:
//memcpy.c
#include
#include
main()
{
char*s="GoldenGlobalView";
chard[20];
clrscr();
memcpy(d,s,strlen(s));
d[strlen(s)]=0;
printf("%s",d);
getchar();
return0;
}
memicmp
原型:
externintmemicmp(void*buf1,void*buf2,unsignedintcount);
用法:
#include
功能:
比较内存区域buf1和buf2的前count个字节但不区分字母的大小写。
说明:
memicmp同memcmp的唯一区别是memicmp不区分大小写字母。
当buf1当buf1=buf2时,返回值=0
当buf1>buf2时,返回值>0
举例:
//memicmp.c
#include
#include
main()
{
char*s1="Hello,Programmers!
";
char*s2="Hello,programmers!
";
intr;
clrscr();
r=memicmp(s1,s2,strlen(s1));
if(!
r)
printf("s1ands2areidentical");
else
if(r<0)
printf("s1lessthans2");
else
printf("s1greaterthans2");
getchar();
return0;
}
memmove
原型:
externvoid*memmove(void*dest,constvoid*src,unsignedintcount);
用法:
#include
功能:
由src所指内存区域复制count个字节到dest所指内存区域。
说明:
src和dest所指内存区域可以重叠,但复制后src内容会被更改。
函数返回指向dest的指针。
举例:
//memmove.c
#include
#include
main()
{
char*s="GoldenGlobalView";
clrscr();
memmove(s,s+7,strlen(s)-7);
s[strlen(s)-7]=0;
printf("%s",s);
getchar();
return0;
}
memset
原型:
externvoid*memset(void*buffer,intc,intcount);
用法:
#include
功能:
把buffer所指内存区域的前count个字节设置成字符c。
说明:
返回指向buffer的指针。
举例:
//memset.c
#include
#include
main()
{
char*s="GoldenGlobalView";
clrscr();
memset(s,'G',6);
printf("%s",s);
getchar();
return0;
}
movmem
原型:
externvoidmovmem(void*src,void*dest,unsignedintcount);
用法:
#include
功能:
由src所指内存区域复制count个字节到dest所指内存区域。
说明:
src和dest所指内存区域可以重叠,但复制后src内容会被更改。
函数返回指向dest的指针。
举例:
//movmem.c
#include
#include
main()
{
char*s="GoldenGlobalView";
clrscr();
movmem(s,s+7,strlen(s)-7);
s[strlen(s)-7]=0;
printf("%s",s);
getchar();
return0;
}
setmem
原型:
externvoidsetmem(void*buf,unsignedintcount,charch);
用法:
#include
功能:
把buf所指内存区域前count个字节设置成字符ch。
说明:
返回指向buf的指针。
举例:
//setmem.c
#include
#include
main()
{
char*s="GoldenGlobalView";
clrscr();
setmem(s,6,'G');
printf("%s",s);
getchar();
return0;
}
stpcpy
原型:
externchar*stpcpy(char*dest,char*src);
用法:
#include
功能:
把src所指由NULL结束的字符串复制到dest所指的数组中。
说明:
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest结尾处字符(NULL)的指针。
举例:
//stpcpy.c
#include
#include
main()
{
char*s="GoldenGlobalView";
chard[20];
clrscr();
stpcpy(d,s);
printf("%s",d);
getchar();
return0;
}
strcat
原型:
externchar*strcat(char*dest,char*src);
用法:
#include
功能:
把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明:
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
举例:
//strcat.c
#include
#include
main()
{
chard[20]="GoldenGlobal";
char*s="View";
clrscr();
strcat(d,s);
printf("%s",d);
getchar();
return0;
}
strchr
原型:
externchar*strchr(char*s,charc);
用法:
#include
功能:
查找字符串s中首次出现字符c的位置
说明:
返回首次出现c的位置的指针,如果s中不存在c则返回NULL。
举例:
//strchr.c
#include
#include
main()
{
char*s="GoldenGlobalView";
char*p;
clrscr();
strchr(s,'V');
if(p)
printf("%s",p);
else
printf("NotFound!
");
getchar();
return0;
}
strcmp
原型:
externintstrcmp(char*s1,char*s2);
用法:
#include
功能:
比较字符串s1和s2。
说明:
当s1当s1=s2时,返回值=0
当s1>s2时,返回值>0
举例:
//strcmp.c
#include
#include
main()
{
char*s1="Hello,Programmers!
";
char*s2="Hello,programmers!
";
intr;
clrscr();
r=strcmp(s1,s2);
if(!
r)
printf("s1ands2areidentical");
else
if(r<0)
printf("s1lessthans2");
else
printf("s1greaterthans2");
getchar();
return0;
}
stricmp,strcmpi
原型:
externintstricmp(char*s1,char*s2);
用法:
#include
功能:
比较字符串s1和s2,但不区分字母的大小写。
说明:
strcmpi是到stricmp的宏定义,实际未提供此函数。
当s1当s1=s2时,返回值=0
当s1>s2时,返回值>0
举例:
//stricmp.c
#include
#include
main()
{
char*s1="Hello,Programmers!
";
char*s2="Hello,programmers!
";
intr;
clrscr();
r=stricmp(s1,s2);
if(!
r)
printf("s1ands2areidentical");
else
if(r<0)
printf("s1lessthans2");
else
printf("s1greaterthans2");
getchar();
return0;
}
strcpy
原型:
externchar*strcpy(char*dest,char*src);
用法:
#include
功能:
把src所指由NULL结束的字符串复制到dest所指的数组中。
说明:
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
举例:
//strcpy.c
#include
#include
main()
{
char*s="GoldenGlobalView";
chard[20];
clrscr();
strcpy(d,s);
printf("%s",d);
getchar();
return0;
}
strcspn
原型:
externintstrcspn(char*s1,char*s2);
用法:
#include
功能:
在字符串s1中搜寻s2中所出现的字符。
说明:
返回第一个出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度。
举例:
//strcspn.c
#include
#include
main()
{
char*s="GoldenGlobalView";
char*r="new";
intn;
clrscr();
n=strcspn(s,r);
printf("Thefirstcharbothins1ands2is:
%c",s[n]);
getchar();
return0;
}
strcspn
原型:
externintstrcspn(char*s1,char*s2);
用法:
#include
功能:
在字符串s1中搜寻s2中所出现的字符。
说明:
返回第一个出现的字符在s1中的下标值,亦即在s1中出现而s2中没有出现的子串的长度。
举例:
//strcspn.c
#include
#include
main()
{
char*s="GoldenGlobalView";
char*r="new";
intn;
clrscr();