string 具体函数 汇集.docx

上传人:b****8 文档编号:9747452 上传时间:2023-02-06 格式:DOCX 页数:16 大小:22.84KB
下载 相关 举报
string 具体函数 汇集.docx_第1页
第1页 / 共16页
string 具体函数 汇集.docx_第2页
第2页 / 共16页
string 具体函数 汇集.docx_第3页
第3页 / 共16页
string 具体函数 汇集.docx_第4页
第4页 / 共16页
string 具体函数 汇集.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

string 具体函数 汇集.docx

《string 具体函数 汇集.docx》由会员分享,可在线阅读,更多相关《string 具体函数 汇集.docx(16页珍藏版)》请在冰豆网上搜索。

string 具体函数 汇集.docx

string具体函数汇集

@函数名称:

   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 string[10];

  char *str1="abcdefghi";

  strcpy(string,str1);

  printf("the string is:

%s\n",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 string[10];

  char *str1="abcdefghi";

  strncpy(string,str1,3);

  string[3]='\0';

  printf("%s",string);

  return 0;

}

@函数名称:

   strcat

函数原型:

   char* strcat(char * str1,char * str2);

函数功能:

   把字符串str2接到str1后面,str1最后的'\0'被取消

函数返回:

   str1

参数说明:

所属文件:

   

#include 

#include 

int main()

{

  char buffer[80];

  strcpy(buffer,"Hello ");

  strcat(buffer,"world");

  printf("%s\n",buffer);

  return 0;

}

@函数名称:

   strncat

函数原型:

   char *strncat(char *dest, const char *src, size_t maxlen)

函数功能:

   将字符串src中前maxlen个字符连接到dest中

函数返回:

参数说明:

所属文件:

   

#include 

#include 

char buffer[80];

int main()

{

  strcpy(buffer,"Hello ");

  strncat(buffer,"world",8);

  printf("%s\n",buffer);

  strncat(buffer,"*************",4);

  printf("%s\n",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(ptr>0)

    printf("buffer 2 is greater than buffer 1\n");

  else

    printf("buffer 2 is less than buffer 1\n");

  ptr=strcmp(buf2, buf3);

  if(ptr>0)

    printf("buffer 2 is greater than buffer 3\n");

  else

    printf("buffer 2 is less than buffer 3\n");

  return 0;

}

@函数名称:

   strncmp

函数原型:

   int strncmp(char *str1,char *str2,int count)

函数功能:

   对str1和str2中的前count个字符按字典顺序比较

函数返回:

   小于0:

str1

str1=str2,大于0:

str1>str2

参数说明:

   str1,str2-待比较的字符串,count-比较的长度

所属文件:

   

#include 

#include 

int main()

{

  int ptr; 

   char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";

∙2008-11-2310:

17

∙回复

∙天空LP之城

∙0位粉丝

2楼

  ptr=strncmp(buf2,buf1,3);

  if (ptr>0)

    printf("buffer 2 is greater than buffer 1");

  else

    printf("buffer 2 is less than buffer 1");

    ptr=strncmp(buf2,buf3,3);

  if (ptr>0)

    printf("buffer 2 is greater than buffer 3");

  else

    printf("buffer 2 is less than buffer 3");

  return(0);

}

@函数名称:

   strpbrk

函数原型:

   char *strpbrk(const char *s1, const char *s2)

函数功能:

   得到s1中第一个“同时也出现在s2中”字符的位置指针

函数返回:

   位置指针

参数说明:

所属文件:

   

#include 

#include 

int main()

{

char *p="Find all vowels";

while(p)

{

  printf("%s\n",p);

  p=strpbrk(p+1,"aeiouAEIOU");

}

return 0;

}

@函数名称:

   strcspn

函数原型:

   int strcspn(const char *s1, const char *s2)

函数功能:

   统计s1中从头开始直到第一个“来自s2中的字符”出现的长度

函数返回:

   长度

参数说明:

所属文件:

   

#include 

#include 

int main()

{

  printf("%d\n",strcspn("abcbcadef","cba"));

  printf("%d\n",strcspn("xxxbcadef","cba"));

  printf("%d\n",strcspn("123456789","cba"));

  return 0;

}

@函数名称:

   strspn

函数原型:

   int strspn(const char *s1, const char *s2)

函数功能:

   统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度

函数返回:

   位置指针

参数说明:

所属文件:

   

#include 

#include 

#include 

int main()

{

  printf("%d\n",strspn("out to lunch","aeiou"));

  printf("%d\n",strspn("out to lunch","xyz"));

  return 0;

}

@函数名称:

   strchr

函数原型:

   char* strchr(char* str,char ch);

函数功能:

   找出str指向的字符串中第一次出现字符ch的位置

函数返回:

   返回指向该位置的指针,如找不到,则返回空指针

参数说明:

   str-待搜索的字符串,ch-查找的字符

所属文件:

   

#include 

#include 

int main()

{

  char string[15];

  char *ptr, c='r';

  strcpy(string, "This is a string");

  ptr=strchr(string, c);

  if (ptr)

    printf("The character %c is at position:

 %d\n",c,ptr-string);

  else

    printf("The character was not found\n");

  return 0;

}

@函数名称:

   strrchr

函数原型:

   char *strrchr(const char *s, int c)

函数功能:

   得到字符串s中最后一个含有c字符的位置指针

函数返回:

   位置指针

参数说明:

所属文件:

   

#include 

#include 

int main()

{

  char string[15];

  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:

%s\n",ptr);

  return 0;

}

@函数名称:

   strrev 

 函数原型:

   char *strrev(char *s)

函数功能:

   将字符串中的所有字符颠倒次序排列

∙2008-11-2310:

17

∙回复

∙天空LP之城

∙0位粉丝

3楼

函数返回:

   指向s的指针 

参数说明:

所属文件:

   

#include 

#include 

int main()

{

  char *forward="string";

  printf("Before strrev():

%s",forward);

  strrev(forward);

  printf("After strrev():

 %s",forward);

  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;

}

@函数名称:

   strset

函数原型:

   char *strset(char *s, int ch)

函数功能:

   将字符串s中所有字符设置为ch的值

函数返回:

   指向s的指针 

参数说明:

所属文件:

   

#include 

#include 

int main()

{

  char string[10]="123456789";

  char symbol='c';

  printf("Before strset():

 %s", string);

  strset(string, symbol);

  printf("After strset():

 %s", string);

  return 0;

}

@函数名称:

   strtok

函数原型:

   char *strtok(char *s1, const char *s2)

函数功能:

   分解s1字符串为用特定分隔符分隔的多个字符串(一般用于将英文句分解为单词)

函数返回:

   字符串s1中首次出现s2中的字符前的子字符串指针

参数说明:

   s2一般设置为s1中的分隔字符

        规定进行子调用时(即分割s1的第二、三及后续子串)第一参数必须是NULL

        在每一次匹配成功后,将s1中分割出的子串位置替换为NULL(摘下链中第一个环),因此s1被破坏了

        函数会记忆指针位置以供下一次调用

        

所属文件:

   

#include 

#include 

int main()

{

  char *p;

  char *buffer;

  char *delims={ " .," };

  buffer=strdup("Find words, all of them.");

  printf("%s\n",buffer);

  p=strtok(buffer,delims);

  while(p!

=NULL){

    printf("word:

 %s\n",p);

    p=strtok(NULL,delims);

  }

  printf("%s\n",buffer);

  return 0;

}

@函数名称:

   strupr

函数原型:

   char *strupr(char *s)

函数功能:

   将字符串s中的字符变为大写

函数返回:

参数说明:

所属文件:

   

#include 

#include 

int main()

{

  char *string="abcdefghijklmnopqrstuvwxyz",*ptr;

  ptr=strupr(string);

  printf("%s",ptr);

  return 0;

}

@函数名称:

   strlwr

函数原型:

   char *strlwr(char *s)

函数功能:

   将字符串中的字符变为小写字符

函数返回:

   指向s的指针

参数说明:

所属文件:

   

#include

int main()

{

  char str[]="HOW TO SAY?

";

  printf("%s",strlwr(str));

  return 0;

}

@函数名称:

   strlen

函数原型:

   unsigned int strlen(char * str);

函数功能:

   统计字符串str中字符的个数(不包括终止符'\0')

函数返回:

   返回字符串的长度.

参数说明:

所属文件:

   

#include 

#include

int main()

{

  char str[]="how are you!

";

  printf("the lence is:

%d\n",strlen(str));

  return 0;

}

@函数名称:

   strerror

函数原型:

   char *strerror(int errnum)

函数功能:

   得到错误信息的内容信息 

 函数返回:

   错误提示信息字符串指针

参数说明:

   errnum-错误编号

所属文件:

   

∙2008-11-2310:

17

∙回复

∙天空LP之城

∙0位粉丝

4楼

#include 

#include 

int main()

{

  char *buffer;

  buffer=strerror(errno);

  printf("Error:

 %s",buffer);

  return 0;

}

@函数名称:

   memcpy

函数原型:

   void *memcpy(void *dest, const void *src, size_t n)

函数功能:

   字符串拷贝

函数返回:

   指向dest的指针

参数说明:

   src-源字符串,n-拷贝的最大长度

所属文件:

   ,

#include 

#include 

int main()

{

  char src[]="******************************";

  char dest[]="abcdefghijlkmnopqrstuvwxyz0123456709";

  char *ptr;

  printf("destination before memcpy:

%s\n",dest);

  ptr=memcpy(dest,src,strlen(src));

  if (ptr)

    printf("destination after memcpy:

%s\n",dest);

  else

    printf("memcpy failed");

  return 0;

}

@函数名称:

   memccpy

函数原型:

   void *memccpy(void *dest, const void *src, int c, size_t n)

函数功能:

   字符串拷贝,到指定长度或遇到指定字符时停止拷贝

函数返回:

参数说明:

   src-源字符串指针,c-中止拷贝检查字符,n-长度,dest-拷贝底目的字符串指针

所属文件:

   ,

#include 

#include 

int main()

{

  char *src="This is the source string";

  char dest[50];

  char *ptr;

  ptr=memccpy(dest,src,'c',strlen(src));

  if (ptr)

  {

    *ptr='\0';

    printf("The character was found:

%s",dest);

  }

  else

    printf("The character wasn't found");

  re

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1