常见的C字符串处理函数的源代码

#include <stdio.h>

#include <assert.h>

char *strcpy(char *strDest,const char *strSrc) //复制字符串

{

  assert((strDest!=NULL)&&(strSrc!=NULL));

  char *address = strDest; 

  while((*strDest++=*strSrc++)!='\0')

  NULL; //空语句

return address;

}

int strcmp(const char *s1,const char * s2)  //比较字符串

  assert(s1!=NULL&&s2!=NULL); 

  while(*s1&&*s2&&*s1==*s2)

   {   

     s1++;   

     s2++; 

   }  

  return (*s1-*s2);

}

char *strcat(char *strDes, const char *strSrc)//连接字符串

{

   assert((strDes!=NULL)&&(strSrc!=NULL));

    char *address = strDes;

   while(*strDes!='\0')  //将原字符串指针移到最后一个字符

    strDes++; 

   while((*strDes++=*strSrc++)!='\0')

     NULL;

   return address;

}

int strlen(const char *str) //求字符串长度

   assert(str!=NULL); 

   int len = 0; 

   while(*str++!='\0')

   {   

     len++;

   } 

  return len;

}

int main(int argc, char* argv[])

{

   char a[10];

   strcpy(a,"hello world");

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

   char s1[]="abcd";

   char s2[]="abcd"; 

   int res = strcmp(s1,s2); 

   printf("%d\n",res);

   char b[]="hello";

   char c[]="world";

   char d[20];

   strcpy(d,b);

   strcat(d,c);

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

   int len = strlen(a); 

   printf("%d\n",len);

    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值