C语言几种常用的string函数

#include<stdio.h>
#include<string.h>
#include<assert.h>
char* strcpy(char * destination, const char * source );
char* strcat ( char * destination, const char * source );
int   strcmp ( const char * str1, const char * str2 );
int   mystrncmp( const char *string1, const char *string2, size_t count );
char* mystrncpy( char *strDest, const char *strSource, size_t count ); 
char* mystrncat( char *strDest, const char *strSource, size_t count );
//字符串拷贝函数
char* mystrcpy(char* destination,const char*source){
	assert(destination!=NULL&&source!=NULL);
	char* dest=destination;
	const char* src=source;
	while(*src!='\0'){
		*dest++=*src++;
	}
	*dest=*src;
	return destination;
}
//字符串连接函数
char* mystrcat(char*destination,const char*source){
	assert(destination!=NULL&&source!=NULL);
	char* dest=destination;
	const char * src=source;
	while(*dest!='\0'){
		dest++;
	}
	while(*src!='\0'){
	   *dest++=*src++;
	}
    *dest=*src;
	return destination;
}
//字符串大小比较函数
int mystrcmp(const char *destination,const char* source){
   assert(destination!=NULL&&source!=NULL);
   	while(*destination!='\0'||*source!='\0'){
   		if((*destination-*source)!=0)
   		break;
   		destination++;
   		source++;
	   }
	   return *destination-*source ;	
}
//n个字符串比较 
int my_strncmp( const char *string1, const char *string2, size_t count )
{
	assert(string1!=NULL && string2!=NULL);
	while(count-- != 0)
	{
		if(*string1 - *string2 != 0)
			break;
		string1++;
		string2++;
	}
if(count==0)
	return *--string1 - *--string2;
	else
	return *string1-*string2;
}
//拷贝n个字符 
char* mystrncpy( char *string1, const char *string2, size_t count ){
	assert(string1!=NULL&&string2!=NULL);
	char* dest=string1;
	const char* src=string2;
	while(count--!=0){
		*dest++=*src++;
	}
	return string1;	
}
//连接n个字符 
char* mystrncat( char *string1, const char *string2, size_t count ){
	assert(string1!=NULL&&string2!=NULL);
	char* dest=string1;
	const char*src=string2;
	while(*dest!='\0'){
		dest++;
	}
	while(count--!=0){
     *dest++=*src++;
		}
		*dest='\0';
		return string1;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhpan666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值