`
xiebh
  • 浏览: 603907 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

C语言中有关string的函数详解

阅读更多

@函数名称:   strdup

函数原型:   char *strdup(const char *s)
函数功能:   字符串拷贝,目的空间由该函数分配
函数返回:   指向拷贝后的字符串指针
参数说明:   src-待拷贝的源字符串
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
#include <alloc.h>
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的指针
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
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-拷贝的字符个数
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
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
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>

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中
函数返回:
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>

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.
函数返回:   str1<str2,返回负数; str1=str2,返回 0; str1>str2,返回正数.
参数说明:
所属文件:   <string.h>

#include <string.h>
#include <stdio.h>
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<str2,等于0:str1=str2,大于0:str1>str2
参数说明:   str1,str2-待比较的字符串,count-比较的长度
所属文件:   <string.h>

#include <string.h>
#include <stdio.h>
int main()
{
  int ptr; 
   char *buf1="aaabbb",*buf2="bbbccc",*buf3="ccc";
  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中”字符的位置指针
函数返回:   位置指针
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
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中的字符”出现的长度
函数返回:   长度
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>

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中的字符”出现的长度
函数返回:   位置指针
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
#include <alloc.h>
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-查找的字符
所属文件:   <string.h>

#include <string.h>
#include <stdio.h>
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字符的位置指针
函数返回:   位置指针
参数说明:
所属文件:   <string.h>

#include <string.h>
#include <stdio.h>
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的串结束符)
函数返回:   返回该位置的指针,如找不到,返回空指针
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
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)
函数功能:   将字符串中的所有字符颠倒次序排列
函数返回:   指向s的指针
参数说明:
所属文件:   <string.h>

#include <string.h>
#include <stdio.h>
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的指针
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
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的指针
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
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被破坏了
        函数会记忆指针位置以供下一次调用
       
所属文件:   <string.h>

#include <string.h>
#include <stdio.h>
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中的字符变为大写
函数返回:
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include <string.h>
int main()
{
  char *string="abcdefghijklmnopqrstuvwxyz",*ptr;
  ptr=strupr(string);
  printf("%s",ptr);
  return 0;
}



@函数名称:   strlwr
函数原型:   char *strlwr(char *s)
函数功能:   将字符串中的字符变为小写字符
函数返回:   指向s的指针
参数说明:
所属文件:   <string.h>

#include<string.h>
int main()
{
  char str[]="HOW TO SAY?";
  printf("%s",strlwr(str));
  return 0;
}



@函数名称:   strlen
函数原型:   unsigned int strlen(char * str);
函数功能:   统计字符串str中字符的个数(不包括终止符'\0')
函数返回:   返回字符串的长度.
参数说明:
所属文件:   <string.h>

#include <stdio.h>
#include<string.h>
int main()
{
  char str[]="how are you!";
  printf("the lence is:%d\n",strlen(str));
  return 0;
}



@函数名称:   strerror
函数原型:   char *strerror(int errnum)
函数功能:   得到错误信息的内容信息
函数返回:   错误提示信息字符串指针
参数说明:   errnum-错误编号
所属文件:   <string.h>

#include <stdio.h>
#include <errno.h>
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-拷贝的最大长度
所属文件:   <string.h>,<mem.h>

#include <stdio.h>
#include <string.h>
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-拷贝底目的字符串指针
所属文件:   <string.h>,<mem.h>

#include <string.h>
#include <stdio.h>
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");
  return 0;
}



@函数名称:   memchr
函数原型:   void *memchr(const void *s, int c, size_t n)
函数功能:   在字符串中第开始n个字符中寻找某个字符c的位置
函数返回:   返回c的位置指针,返回NULL时表示未找到
参数说明:   s-要搜索的字符串,c-要寻找的字符,n-指定长度
所属文件:   <string.h>,<mem.h>

#include <string.h>
#include <stdio.h>
int main()
{
  char str[17];
  char *ptr;
  strcpy(str,"This is a string");
  ptr=memchr(str,'r',strlen(str));
  if (ptr)
  printf("The character 'r' is at position: %d",ptr-str);
  else
  printf("The character was not found");
  return 0;
}



@函数名称:   memcmp
函数原型:   int memcmp(const void *s1, const void *s2,size_t n)
函数功能:   按字典顺序比较两个串s1和s2的前n个字节
函数返回:   <0,=0,>0分别表示s1<,=,>s2
参数说明:   s1,s2-要比较的字符串,n-比较的长度
所属文件:   <string.h>,<mem.h>

#include <stdio.h>
#include <string.h>
int main() 
{ 
  char *buf1="ABCDE123"; 
  char *buf2="abcde456"; 
  int stat; 
  stat=memcmp(buf1,buf2,5); 
  printf("The strings to position 5 are "); 
  if(stat) printf("not "); 
  printf("the same\n"); 
  return 0; 
} 




@函数名称:   memicmp
函数原型:   int memicmp(const void *s1, const void *s2, size_t n)
函数功能:   按字典顺序、不考虑字母大小写对字符串s1,s2前n个字符比较
函数返回:   <0,=0,>0分别表示s1<,=,>s2
参数说明:   s1,s2-要比较的字符串,n-比较的长度
所属文件:   <string.h>,<mem.h>

#include <stdio.h>
#include <string.h>
int main()
{
  char *buf1="ABCDE123";
  char *buf2="abcde456";
  int stat;
  stat=memicmp(buf1,buf2,5);
  printf("The strings to position 5 are ");
  if(stat) printf("not");
  printf("the same");
  return 0;
}



@函数名称:   memmove
函数原型:   void *memmove(void *dest, const void *src, size_t n)
函数功能:   字符串拷贝
函数返回:   指向dest的指针
参数说明:   src-源字符串,n-拷贝的最大长度
所属文件:   <string.h>,<mem.h>

#include <string.h>
#include <stdio.h>
int main()
{
  char dest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
  printf("destination prior to memmove:%s\n",dest);
  memmove(dest+1,dest,35);
  printf("destination after memmove:%s",dest);
  return 0;
}



@函数名称:   memset
函数原型:   void *memset(void *s, int c, size_t n)
函数功能:   字符串中的n个字节内容设置为c
函数返回:
参数说明:   s-要设置的字符串,c-设置的内容,n-长度
所属文件:   <string.h>,<mem.h>

#include <string.h>
#include <stdio.h>
#include <mem.h>
int main()
{
  char buffer[]="Hello world";
  printf("Buffer before memset:%s\n",buffer);
  memset(buffer,'*',strlen(buffer)-1);
  printf("Buffer after memset:%s",buffer);
  return 0;
} 
分享到:
评论

相关推荐

    C语言string函数详解.doc

    C语言string函数详解.doc

    基于C语言string函数的详解

    本篇文章是对C语言中string函数进行了详细的分析介绍,需要的朋友参考下

    305-字符串函数string.h应用举例(51单片机C语言实例Proteus仿真和代码)

    305-字符串函数string.h应用举例(51单片机C语言实例Proteus仿真和代码)305-字符串函数string.h应用举例(51单片机C语言实例Proteus仿真和代码)305-字符串函数string.h应用举例(51单片机C语言实例Proteus仿真和代码)...

    C语言 用指针作为函数返回值详解

    C语言允许函数的返回值是一个指针(地址),我们将这样的函数称为指针函数。下面的例子定义了一个函数 strlong(),用来返回两个字符串中较长的一个: #include #include &lt;string&gt; char *strlong(char *str1, char...

    详解C语言中index()函数和rindex()函数的用法

    C语言index()函数:查找字符串并返回首次出现的位置 相关函数:rindex, srechr, strrchr 头文件:#include &lt;string&gt; 定义函数: char * index(const char *s, int c); 函数说明:index()用来找出参数s 字符串中第...

    C语言正则表达式详解 regcomp() regexec() regfree()详解1

    2. int regexec (regex_t *compiled, char *string, size_t nmatch, regmatch_t match

    详解C语言中的memset()函数

    C语言memset()函数:将内存的前n个字节设置为特定的值 头文件: #include &lt;string&gt; memset() 函数用来将指定内存的前n个字节设置为特定的值,其原型为: void * memset( void * ptr, int value, size_t num ); ...

    C语言putenv()函数和getenv()函数的使用详解

    C语言putenv()函数:改变或增加环境变量 头文件: #include4 定义函数: int putenv(const char * string); 函数说明:putenv()用来改变或增加环境变量的内容. 参数string 的格式为name=value, 如果该环境变量...

    详解C语言中strcpy()函数与strncpy()函数的使用

    C语言strcpy()函数:复制字符串 头文件:#include &lt;string&gt; 定义函数: char *strcpy(char *dest, const char *src); 函数说明:strcpy()会将参数src 字符串拷贝至参数dest 所指的地址。 返回值:返回参数dest 的...

    C语言参考手册 C标准 (内含6个chm格式手册)

    内含:C参考手册.chm(最全的一个) 、C函数查询.chm 、C语言库函数速查手册.chm 、C语言100例.chm、C语言标准库函数大全.chm、C语言库函数使用大全CHM版.chm、 这6个是我找了好久才找到的,各有各的好处,3个互补十分...

    详解C++中string的用法和例子

    同时C++的算法库对string也有着很好的支持,而且string还和c语言的字符串之间有着良好的接口。虽然也有一些弊端,但是瑕不掩瑜。 其中使用的代码多数都是来自cpp官网,因为例子非常全。 声明和初始化方法: 想使用...

    嵌入式web服务器boa_C语言/Python + HTML + javascript + ajax 代码实例例子

    CGI getenv函数的参数详解: http://www.cnblogs.com/ser0632/p/5498228.html s = getenv("环境变量名"); 取得环境变量内容 putenv改变或增加环境变量 int putenv(const char * string); setenv(改变或增加环境变量...

    C语言中的sscanf()函数使用详解

    sscanf() – 从一个字符串中读进与指定格式相符的数据.  函数原型:  Int sscanf( string str, string fmt, mixed var1, mixed var2 ... );  int scanf( const char *format [,argument]... );  说明:  ...

    C语言 全局变量和局部变量详解及实例

    C语言 全局变量和局部变量详解 核心内容: 1、局部变量和全局变量 变量按照作用域分为:全局变量和局部变量 全局变量的作用域:从定义位置开始到下面整个程序结束。 局部变量的作用域:在一个函数内部定义的...

    C语言中字符串实现正序与逆序实例详解

    C语言中字符串实现逆序实例详解 字符串逆序和正序的实现代码: #include #include #include #include #include &lt;string&gt; /*定义*/ typedef struct node { char c; struct node *llink,*rlink; }stud; /*建立...

    C语言中使用快速排序算法对元素排序的实例详解

    调用C语言的快速排序算法qsort(); #include #include #include&lt;string&gt; #define SIZE 100 //从小到大排序 int comp1(const void *x,const void *y) { return *(int *)x - *(int *)y; } //从大到小排序 int comp2...

    C语言中的const和free用法详解

    注意:C语言中的const和C++中的const是有区别的,而且在使用VS编译测试的时候。如果是C的话,请一定要建立一个后缀为C的文件,不要是CPP的文件。因为,两个编译器会有差别的。 一、C语言中的const比较常见的用法,...

    如何用C语言编写PHP扩展的详解

    1:预定义在home目录,也可以其他任意目录,写一个文件,例如caleng_module.def内容是你希望定义的函数名以及参数:int a(int x,int y)string b(string str,int n)2:到php源码目录的ext目录#cd /usr/local/...

    详解c语言中的 strcpy和strncpy字符串函数使用

    strcpy 和strcnpy函数——字符串复制函数。 1.strcpy函数 函数原型:char *strcpy(char *dst,char const *src) 必须保证dst字符的空间足以保存src字符,否则多余的字符仍然被复制,覆盖原先存储在数组后面的内存...

Global site tag (gtag.js) - Google Analytics