c和c++中的头文件String.h包含的常用函数及简要分析.docx_第1页
c和c++中的头文件String.h包含的常用函数及简要分析.docx_第2页
c和c++中的头文件String.h包含的常用函数及简要分析.docx_第3页
c和c++中的头文件String.h包含的常用函数及简要分析.docx_第4页
c和c++中的头文件String.h包含的常用函数及简要分析.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

String.h是c和c+中常用的头文件、很多常用的函数都包含在这个头文件中,所以大致了解这个头文件以及里面的函数是我们学习c和c+ 过程中必不可少的一部分、在这里我对一下常用函数做了一些简单的介绍以及举例说明。希望对大家有一定的帮助。string.h中包含的函数函数名称: strdup函数原型: char *strdup(const char *s)函数功能: 字符串拷贝,目的空间由该函数分配 函数返回: 指向拷贝后的字符串指针参数说明: src-待拷贝的源字符串所属文件: #include #include #include int main() char *dup_str, *string=abcde; dup_str=strdup(string); printf(%s, dup_str); free(dup_str); return 0;函数名称: strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把str2指向的字符串拷贝到str1中去函数返回: 返回str1,即指向str1的指针参数说明:所属文件: #include #include int main() char string10; char *str1=abcdefghi; strcpy(string,str1); printf(the string is:%sn,string); return 0;函数名称: strncpy函数原型: char *strncpy(char *dest, const char *src,int count)函数功能: 将字符串src中的count个字符拷贝到字符串dest中去函数返回: 指向dest的指针参数说明: dest-目的字符串,src-源字符串,count-拷贝的字符个数所属文件: #include #include int main() char string10; char *str1=abcdefghi; strncpy(string,str1,3); string3=0; printf(%s,string); return 0;函数名称: strcat函数原型: char* strcat(char * str1,char * str2);函数功能: 把字符串str2接到str1后面,str1最后的0被取消函数返回: str1参数说明:所属文件: #include #include int main() char buffer80; strcpy(buffer,Hello ); strcat(buffer,world); printf(%sn,buffer); return 0;函数名称: strncat函数原型: char *strncat(char *dest, const char *src, size_t maxlen)函数功能: 将字符串src中前maxlen个字符连接到dest中函数返回:参数说明:所属文件: #include #include char buffer80;int main() strcpy(buffer,Hello ); strncat(buffer,world,8); printf(%sn,buffer); strncat(buffer,*,4); printf(%sn,buffer); return 0;函数名称: strcmp函数原型: int strcmp(char * str1,char * str2);函数功能: 比较两个字符串str1,str2.函数返回: str1str2,返回正数. 参数说明:所属文件: #include #include int main() char *buf1=aaa, *buf2=bbb, *buf3=ccc; int ptr; ptr=strcmp(buf2, buf1); if(ptr0) printf(buffer 2 is greater than buffer 1n); else printf(buffer 2 is less than buffer 1n); ptr=strcmp(buf2, buf3); if(ptr0) printf(buffer 2 is greater than buffer 3n); else printf(buffer 2 is less than buffer 3n); return 0;函数名称: strncmp函数原型: int strncmp(char *str1,char *str2,int count)函数功能: 对str1和str2中的前count个字符按字典顺序比较函数返回: 小于0:str1str2参数说明: str1,str2-待比较的字符串,count-比较的长度所属文件: #include #include int main() int ptr; char *buf1=aaabbb,*buf2=bbbccc,*buf3=ccc; ptr=strncmp(buf2,buf1,3); if (ptr0) printf(buffer 2 is greater than buffer 1); else printf(buffer 2 is less than buffer 1); ptr=strncmp(buf2,buf3,3); if (ptr0) printf(buffer 2 is greater than buffer 3); else printf(buffer 2 is less than buffer 3); return(0);函数名称: strchr函数原型: char* strchr(char* str,char ch);函数功能: 找出str指向的字符串中第一次出现字符ch的位置函数返回: 返回指向该位置的指针,如找不到,则返回空指针参数说明: str-待搜索的字符串,ch-查找的字符所属文件: #include #include int main() char string15; char *ptr, c=r; strcpy(string, This is a string); ptr=strchr(string, c); if (ptr) printf(The character %c is at position: %dn,c,ptr-string); else printf(The character was not foundn); return 0;函数名称: strrchr函数原型: char *strrchr(const char *s, int c)函数功能: 得到字符串s中最后一个含有c字符的位置指针函数返回: 位置指针参数说明:所属文件: #include #include int main() char string15; char *ptr,c=r; strcpy(string,This is a string); ptr=strrchr(string,c); if (ptr) printf(The character %c is at position:%d,c,ptr-string); else printf(The character was not found); return 0;函数名称: strstr函数原型: char* strstr(char* str1,char* str2);函数功能: 找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)函数返回: 返回该位置的指针,如找不到,返回空指针参数说明:所属文件: #include #include int main() char *str1=Open Watcom C/C+,*str2=Watcom,*ptr; ptr=strstr(str1,str2); printf(The substring is:%sn,ptr); return 0;函数名称: strnset函数原型: char *strnset(char *s, int ch, size_t n)函数功能: 将字符串s中前n个字符设置为ch的值函数返回: 指向s的指针参数说明:所属文件: #include #include int main() char *string=abcdefghijklmnopqrstuvwxyz; char letter=x; printf(string before strnset: %s,string); strnset(string,letter,13); printf(string after strnset: %s,string); return 0;函数名称: strrev函数原型: char *strrev(char *s)函数功能: 将字符串中的所有字符颠倒次序排列函数返回: 指向s的指针 参数说明:所属文件: #include #include int main() char *forward=string; printf(Before strrev():%s,forward); strrev(forward); printf(After strrev(): %s,forward); return 0;函数名称: strset函数原型: char *strset(char *s, int ch)函数功能: 将字符串s中所有

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论