C语言常用系统函数--字符串函数

3188阅读 0评论2012-01-05 TOMSYAN
分类:C/C++

bcmp

 

原型:extern int bcmp(const void *s1, const void *s2, int n);

 

用法:#include

 

功能:比较字符串s1s2的前n个字节是否相等

 

说明:如果s1=s2n=0则返回零,否则返回非零值。bcmp不检查NULL

 

举例:

 

/home/cpicsrv/yansp/cstring > cat bcmp.c

//bcmp.c

#include

#include

int main(void)

{

  char *s1="Hello Huateng!";

  char *s2="Hello huateng!";

  int  result;

 

  result=bcmp(s1,s2,6);

  printf("Result=[%d]\n",result);

  if (result==0)

   printf("String \"%s\" and  \"%s\" is  equal in first 6 bytes! \n",s1,s2);

  else

  printf("String \"%s\" and  \"%s\" is  not equal in first 6 bytes! \n",s1,s2);

 

  result=bcmp(s1,s2,13);

  printf("Result=[%d]\n",result);

  if (result==-1)

   printf("String \"%s\"  is less than \"%s\"  in first 13 bytes! \n",s1,s2);

  else

   printf("String \"%s\"  is greater than  \"%s\"  in first 13 bytes! \n",s1,s2);

  

  result=bcmp(s2,s1,13);

  printf("Result=[%d]\n",result);

  if (result==1)

      printf("String \"%s\"  is greater than  \"%s\"  in first 13 bytes! \n",s1,s2);

  else

      printf("String \"%s\"  is less than \"%s\"  in first 13 bytes! \n",s1,s2);

  

  return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall bcmp.c -o bcmp

/home/cpicsrv/yansp/cstring > ./bcmp

Result=[0]

String "Hello Huateng!" and  "Hello huateng!" is  equal in first 6 bytes!

Result=[-1]

String "Hello Huateng!"  is less than "Hello huateng!"  in first 13 bytes!

Result=[1]

String "Hello Huateng!"  is greater than  "Hello huateng!"  in first 13 bytes!

/home/cpicsrv/yansp/cstring >

 

bcopy

 

  原型:extern void bcopy(const void *src, void *dest, int n);

 

  用法:#include

 

  功能:将字符串src的前n个字节复制到dest

 

  说明:bcopy不检查字符串中的空字节NULL,函数没有返回值。

 

  举例:

 

 

/home/cpicsrv/yansp/cstring > cat bcopy.c

//bcopy.c

#include

#include

int main()

{

  char *s1="Hello huateng!";

  char s2[50];

 

  bcopy(s1,s2,5);

  printf("s1=[%s]\n",s1);

  printf("s2=[%s]\n",s2);

 

  return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall bcopy.c -o bcopy

/home/cpicsrv/yansp/cstring > ./bcopy

s1=[Hello huateng!]

s2=[Hello]

/home/cpicsrv/yansp/cstring >

 

bzero

 

  原型:extern void bzero(void *s, int n);

 

  用法:#include

 

  功能:置字节字符串s的前n个字节为零。

 

  说明:bzero无返回值。

 

  举例:

 

/home/cpicsrv/yansp/cstring > cat bzero.c

//bzero.c

#include

#include

int main()

{

  struct

    {

      int a;

      float b;

      char  c[20];

     } huateng;

   huateng.a=10;

   huateng.b=20.322;

   strcpy(huateng.c,"HUATENG");

   printf("a=[%d]\n",huateng.a);

   printf("b=[%f]\n",huateng.b);

   printf("c=[%s]\n",huateng.c);

  

   bzero(&huateng,sizeof(huateng));

  

   printf("a=[%d]\n",huateng.a);

   printf("b=[%f]\n",huateng.b);

   printf("c=[%s]\n",huateng.c);

  

   return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall bzero.c -o bzero

/home/cpicsrv/yansp/cstring > ./bzero

a=[10]

b=[20.322001]

c=[HUATENG]

a=[0]

b=[0.000000]

c=[]

/home/cpicsrv/yansp/cstring >

 

memccpy

1.4.  memccpy

 

  原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count);

 

  用法:#include

 

  功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。

 

  说明:返回指向字符ch后的第一个字符的指针,如果srcn个字节中不存在ch则返回NULL,并且源字符串会被整个复制到目标字符串中。 

 

  举例:

 

/home/cpicsrv/yansp/cstring > cat memccpy.c

//memccpy

#include

#include

int main()

{

  char *s="Hello -Huateng!";

  char d[20],*p;

 

  p=memccpy(d,s,'-',strlen(s));

  printf("Address of p is :[%x]\n",p);

  printf("Address of p is :[%x]\n",d);

  if(p)

  {

    *p='\0';

    printf("string d is :[%s]\n",d);

  }

  else

   printf("Char not found!");

  

 p=memccpy(d,s,'$',strlen(s));

 if (!p)

 printf("Char not found!  string d is :[%s]\n",d);

 

  return 0;

}

/home/cpicsrv/yansp/cstring > gcc -Wall memccpy.c -o  memccpy

memccpy.c: In function `main':

memccpy.c:10: warning: unsigned int format, pointer arg (arg 2)

memccpy.c:11: warning: unsigned int format, pointer arg (arg 2)

/home/cpicsrv/yansp/cstring > ./memccpy

Address of p is :[bff7fbd7]

Address of p is :[bff7fbd0]

string d is :[Hello -]

Char not found!  string d is :[Hello -Huateng]

/home/cpicsrv/yansp/cstring >

上一篇:Pro*C 中 SELECT 数据到数组中注意数组定义的大小
下一篇:OEM:Unable to initialize ssl connection with server, aborting connection attempt