Linux中用指针函数封装Strlen,Strcpy,Strcmp函数,实现strlen,strcpy,strcmp函数的功能

#include<stdio.h>
int value;
int  *my_strlen(char *);
int main(int argc, const char *argv[])
{
	char s1[32] = "123456789";
	int *q = my_strlen(s1);
	printf("%d\n",*q);
	return 0;
}
int *my_strlen(char *p){
	char *ptemp = p;
	while(*ptemp){
		value++;
		ptemp++;
	}
	return &value;

}

1.实现strlen函数功能

#include<stdio.h>

char *my_strcpy(char *p,char *q){
	char *ptemp1 = p;
	char *ptemp2 = q;
	while(*ptemp2){
	*ptemp1 = *ptemp2;
	ptemp2++;
	ptemp1++;
	}
	*ptemp1 = *ptemp2;
	return p;

}

int main(int argc, const char *argv[])
{
	char s1[] = "hello world";
	char s2[] = "abcdhqyj";
	my_strcpy(s1,s2);
	printf("s1 = %s\n",s1);
	printf("s2 = %s\n",s2);
	return 0;
}

2.实现strcpy函数的功能

#include<stdio.h>

int *my_strcmp(char *p,char *q,int *x){
	char *ptemp1 = p;
	char *ptemp2 = q;
	while(*ptemp2){
		if(*ptemp1!=*ptemp2){
			break;
		}
		ptemp1++;
		ptemp2++;
	}
	*x = *ptemp1 - *ptemp2; 	

}

int main(int argc, const char *argv[])
{
	char s1[32] = "abcdhqyj";
	char s2[32] = "abcdhxlloworld";
	int ret1 = 0;
	my_strcmp(s1,s2,&ret1);
	if(ret1 > 0){
		printf("s1>s2\n");
	}else if(ret1<0 ){
		printf("s1<s2\n");
	}else
		printf("s1==s2\n");
	return 0;
}

3.实现strcmp函数的功能

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值