标准c字符和字符串库函数.docx
《标准c字符和字符串库函数.docx》由会员分享,可在线阅读,更多相关《标准c字符和字符串库函数.docx(20页珍藏版)》请在冰豆网上搜索。
标准c字符和字符串库函数
标准c字符和字符串
atof
语法:
#include
doubleatof(constchar*str);
功能:
将字符串str转换成一个双精度数值并返回结果。
参数str必须以有效数字开头,但是允许以“E”或“e”除外的任意非数字字符结尾。
例如:
x=atof("42.0is_the_answer");
x的值为42.0.
相关主题:
atoi()andatol().
atoi
语法:
#include
intatoi(constchar*str);
功能:
将字符串str转换成一个整数并返回结果。
参数str以数字开头,当函数从str中读到非数字字符则结束转换并将结果返回。
例如,
i=atoi("512.035");
i的值为512.
相关主题:
atof()andatol().
atol
语法:
#include
longatol(constchar*str);
功能:
将字符串转换成长整型数并返回结果。
函数会扫描参数str字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时才结束转换,并将结果返回。
例如,
x=atol("1024.0001");
x的值为1024L.
相关主题:
atof()andatoi().
isalnum
语法:
#include
intisalnum(intch);
功能:
如果参数是数字或字母字符,函数返回非零值,否则返回零值。
charc;
scanf("%c",&c);
if(isalnum(c))
printf("Youenteredthealphanumericcharacter%c\n",c);
相关主题:
isalpha(),iscntrl(),isdigit(),isgraph(),isprint(),ispunct(),andisspace().
isalpha
语法:
#include
intisalpha(intch);
功能:
如果参数是字母字符,函数返回非零值,否则返回零值。
charc;
scanf("%c",&c);
if(isalpha(c))
printf("Youenteredaletterofthealphabet\n");
相关主题:
isalnum(),iscntrl(),isdigit(),isgraph(),isprint(),ispunct(),andisspace().
iscntrl
语法:
#include
intiscntrl(intch);
功能:
如果参数是控制字符(0和0x1F之间的字符,或者等于0x7F)函数返回非零值,否则返回零值。
相关主题:
isalnum(),isalpha(),isdigit(),isgraph(),isprint(),ispunct(),andisspace().
isdigit
语法:
#include
intisdigit(intch);
功能:
如果参数是0到9之间的数字字符,函数返回非零值,否则返回零值.
charc;
scanf("%c",&c);
if(isdigit(c))
printf("Youenteredthedigit%c\n",c);
相关主题:
isalnum(),isalpha(),iscntrl(),isgraph(),isprint(),ispunct(),andisspace().
isgraph
语法:
#include
intisgraph(intch);
功能:
如果参数是除空格外的可打印字符(可见的字符),函数返回非零值,否则返回零值。
相关主题:
isalnum(),isalpha(),iscntrl(),isdigit(),isprint(),ispunct(),andisspace().
islower
语法:
#include
intislower(intch);
功能:
如果参数是小写字母字符,函数返回非零值,否则返回零值。
相关主题:
isupper()
isprint
语法:
#include
intisprint(intch);
功能:
如果参数是可打印字符(包括空格),函数返回非零值,否则返回零值。
相关主题:
isalnum(),isalpha(),iscntrl(),isdigit(),isgraph(),ispunct(),andisspace().
ispunct
语法:
#include
intispunct(intch);
功能:
如果参数是除字母,数字和空格外可打印字符,函数返回非零值,否则返回零值。
相关主题:
isalnum(),isalpha(),iscntrl(),isdigit(),isgraph(),isprint(),andisspace().
isspace
语法:
#include
intisspace(intch);
功能:
如果参数是空格类字符(即:
单空格,制表符,垂直制表符,满页符,回车符,新行符),函数返回非零值,否则返回零值。
相关主题:
isalnum(),isalpha(),iscntrl(),isdigit(),isgraph(),andispunct().
isupper
语法:
#include
intisupper(intch);
功能:
如果参数是大写字母字符,函数返回非零值,否则返回零值。
相关主题:
tolower()
isxdigit
语法:
#include
intisxdigit(intch);
功能:
如果参数是十六进制数字字符(即:
A-F,a-f,0-9),函数返回非零值,否则返回零值。
相关主题:
isalnum(),isalpha(),iscntrl(),isdigit(),isgraph(),ispunct(),andisspace().
memchr
语法:
#include
void*memchr(constvoid*buffer,intch,size_tcount);
功能:
函数在buffer指向的数组的count个字符的字符串里查找ch首次出现的位置。
返回一个指针,指向ch在字符串中首次出现的位置,如果ch没有在字符串中找到,返回NULL。
例如:
charnames[]="AlanBobChrisXDave";
if(memchr(names,'X',strlen(names))==NULL)
printf("Didn'tfindanX\n");
else
printf("FoundanX\n");
相关主题:
memcpy()andstrstr().
memcmp
语法:
#include
intmemcmp(constvoid*buffer1,constvoid*buffer2,size_tcount);
功能:
函数比较buffer1和buffer2的前count个字符。
返回值如下:
Value
解释
lessthan0
buffer1islessthanbuffer2
equalto0
buffer1isequaltobuffer2
greaterthan0
buffer1isgreaterthanbuffer2
相关主题:
memchr(),memcpy(),andstrcmp().
memcpy
语法:
#include
void*memcpy(void*to,constvoid*from,size_tcount);
功能:
函数从from中复制count个字符到to中,并返回to指针。
如果to和from重叠,则函数行为不确定。
相关主题:
memmove().
memmove
语法:
#include
void*memmove(void*to,constvoid*from,size_tcount);
功能:
与mencpy相同,不同的是当to和from重叠,函数正常仍能工作。
相关主题:
memcpy().
memset
语法:
#include
void*memset(void*buffer,intch,size_tcount);
功能:
函数拷贝ch到buffer从头开始的count个字符里,并返回buffer指针。
memset()可以应用在将一段内存初始化为某个值。
例如:
memset(the_array,'\0',sizeof(the_array));
这是将一个数组的所以分量设置成零的很便捷的方法。
相关主题:
memcmp(),memcpy(),andmemmove().
strcat
语法:
#include
char*strcat(char*str1,constchar*str2);
功能:
函数将字符串str2连接到str1的末端,并返回指针str1.例如:
printf("Enteryourname:
");
scanf("%s",name);
title=strcat(name,"theGreat");
printf("Hello,%s\n",title);
相关主题:
strchr(),strcmp(),andstrcpy().
strchr
语法:
#include
char*strchr(constchar*str,intch);
功能:
函数返回一个指向str中ch首次出现的位置,当没有在str中找ch到返回NULL。
相关主题:
strpbrk(),strspn(),strstr(),andstrtok().
strcmp
语法:
#include
intstrcmp(constchar*str1,constchar*str2);
功能:
比较字符串str1andstr2,返回值如下:
返回值
解释
lessthan0
str1islessthanstr2
equalto0
str1isequaltostr2
greaterthan0
str1isgreaterthanstr2
例如:
printf("Enteryourname:
");
scanf("%s",name);
if(strcmp(name,"Mary")==0)
printf("Hello,Dr.Mary!
\n");
相关主题:
memcmp(),strchr(),strcpy(),andstrncmp().
strcoll
语法:
#include
intstrcoll(constchar*str1,constchar*str2);
功能:
比较字符串str1和str2,很象strcmp.但是,strcoll()使用在目前环境中由setlocale()设定的次序进行比较。
strcpy
语法:
#include
char*strcpy(char*to,constchar*from);
功能:
复制字符串from中的字符到字符串to,包括空值结束符。
返回值为指针to。
相关主题:
memcpy(),strchr(),strcmp(),strncmp(),andstrncpy().
strcspn
语法:
#include
size_tstrcspn(constchar*str1,constchar*str2);
功能:
函数返回str1开头连续n个字符都不含字符串str2内字符的字符数。
相关主题:
strrchr(),strpbrk(),strstr(),andstrtok().
strerror
语法:
#include
char*strerror(intnum);
功能:
函数返回一个被定义的与某错误代码相关的错误信息。
strlen
语法:
#include
size_tstrlen(char*str);
功能:
函数返回字符串str的长度(即空值结束符之前字符数目)。
相关主题:
memcpy(),strchr(),strcmp(),andstrncmp().
strncat
语法:
#include
char*strncat(char*str1,constchar*str2,size_tcount);
功能:
将字符串from中至多count个字符连接到字符串to中,追加空值结束符。
返回处理完成的字符串。
相关主题:
strcat(),strnchr(),strncmp(),andstrncpy().
strncmp
语法:
#include
intstrncmp(constchar*str1,constchar*str2,size_tcount);
功能:
比较字符串str1和str2中至多count个字符。
返回值如下:
返回值
解释
lessthan0
str1islessthanstr2
equalto0
str1isequaltostr2
greaterthan0
str1isgreaterthanstr2
如果参数中任一字符串长度小于count,那么当比较到第一个空值结束符时,就结束处理。
相关主题:
strcmp(),strnchr(),andstrncpy().
strncpy
语法:
#include
char*strncpy(char*to,constchar*from,size_tcount);
功能:
将字符串from中至多count个字符复制到字符串to中。
如果字符串from的长度小于count,其余部分用'\0'填补。
返回处理完成的字符串。
相关主题:
memcpy(),strchr(),strncat(),andstrncmp().
strpbrk
语法:
#include
char*strpbrk(constchar*str1,constchar*str2);
功能:
函数返回一个指针,它指向字符串str2中任意字符在字符串str1首次出现的位置,如果不存在返回NULL。
相关主题:
strspn(),strrchr(),strstr(),andstrtok().
strrchr
语法:
#include
char*strrchr(constchar*str,intch);
功能:
函数返回一个指针,它指向字符ch在字符串str末次出现的位置,如果匹配失败,返回NULL。
相关主题:
strpbrk(),strspn(),strstr(),strtok(),
strspn
语法:
#include
size_tstrspn(constchar*str1,constchar*str2);
功能:
函数返回字符串str1中第一个不包含于字符串str2的字符的索引。
相关主题:
strpbrk(),strrchr(),strstr(),strtok(),
strstr
语法:
#include
char*strstr(constchar*str1,constchar*str2);
功能:
函数返回一个指针,它指向字符串str2首次出现于字符串str1中的位置,如果没有找到,返回NULL。
相关主题:
strchr(),strcspn(),strpbrk(),strspn(),strtok(),strrchr(),
strtod
语法:
#include
doublestrtod(constchar*start,char**end);
功能:
函数返回带符号的字符串start所表示的浮点型数。
字符串end指向所表示的浮点型数之后的部分。
如果溢出发生,返回HUGE_VAL或-HUGE_VAL。
相关主题:
atof()
strtok
语法:
#include
char*strtok(char*str1,constchar*str2);
功能:
函数返回字符串str1中紧接“标记”的部分的指针,字符串str2是作为标记的分隔符。
如果分隔标记没有找到,函数返回NULL。
为了将字符串转换成标记,第一次调用str1指向作为标记的分隔符。
之后所以的调用str1都应为NULL。
例如:
charstr[]="now#isthetimeforall#goodmentocometothe#aidoftheircountry";
chardelims[]="#";
char*result=NULL;
result=strtok(str,delims);
while(result!
=NULL){
printf("resultis\"%s\"\n",result);
result=strtok(NULL,delims);
}
以上代码的运行结果是:
resultis"now"
resultis"isthetimeforall"
resultis"goodmentocometothe"
resultis"aidoftheircountry"
相关主题:
strchr(),strcspn(),strpbrk(),strrchr(),andstrspn().
strtol
语法:
#include
longstrtol(constchar*start,char**end,intbase);
功能:
函数返回带符号的字符串start所表示的长整型数。
参数base代表采用的进制方式。
指针end指向start所表示的整型数之后的部分。
如果返回值无法用长整型表示,函数则返回LONG_MAX或LONG_MIN.错误发生时,返回零。
相关主题:
atol().
strtoul
语法:
#include
unsignedlongstrtoul(constchar*start,char**end,intbase);
功能:
函数基本等同strtol(),不同的是,它不仅可以返回长整型数,而且可以返回无符号的长整型数。
相关主题:
strtol()
strxfrm
语法:
#include
size_tstrxfrm(char*str1,constchar*str2,size_tnum);
功能:
函数将字符串str2的前num个字符存储到字符串str1中。
如果strcoll()处理字符串str1和旧的字符串str2,返回值和strcmp()的处理结果一样。
相关主题:
strcmp(),strcoll(),
tolower
语法:
#include
inttolower(intch);
功能:
函数字符ch的小写形式。
相关主题:
toupper(),
toupper
语法:
#include
inttoupper(intch);
功能:
函数字符ch的大写形式。
相关主题:
tolower(),