数据结构预科

在堆区申请两个长度为32的空间,实现两个字符串的比较【非库函数实现】

要求:

1> 定义函数,在对区申请空间,两个申请,主函数需要调用2次

2> 定义函数,实现字符串的输入,void input(char *p)

3> 调用函数实现字符串比较,在主函数中输出大小

int my_strcmp(const char *s1,const char *s2)

4> 定义函数,释放空间

cs 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *malloc_p()
{
	char *p=(char *)malloc(32);
	return p;
}

void intput(char *p)
{
	gets(p);
}

int my_strcmp(const char *s1,const char *s2)
{
	int i=0;
	while(s1[i]==s2[i])
	{
		if(s1[i]=='\0')
			break;
		i++;
	}
	int sub=s1[i]-s2[i];
	return sub;
}

void Free(char *p1,char *p2)
{
	free(p1);
	free(p2);
}
int main(int argc, const char *argv[])
{
	char *p1=malloc_p();
	char *p2=malloc_p();
	printf("please enter p1:");
	intput(p1);
	printf("please enter p2:");
	intput(p2);
	printf("结果为:");
	int sub=my_strcmp(p1,p2);
	if(sub>0)
		puts("p1>p2");
	else if(sub<0)
		puts("p1<p2");
	else if(sub==0)
		puts("p1=p2");
	Free(p1,p2);
	return 0;
}
相关推荐
CQ_YM7 小时前
数据结构之单向链表
c语言·数据结构·链表
im_AMBER10 小时前
算法笔记 18 二分查找
数据结构·笔记·学习·算法
C雨后彩虹10 小时前
机器人活动区域
java·数据结构·算法·华为·面试
苏小瀚10 小时前
[算法]---路径问题
数据结构·算法·leetcode
前端之虎陈随易12 小时前
MoonBit内置数据结构详解
数据结构·数据库·redis
CodeByV13 小时前
【算法题】双指针(二)
数据结构·算法
Jasmine_llq16 小时前
《P3811 【模板】模意义下的乘法逆元》
数据结构·算法·线性求逆元算法·递推求模逆元
虹科网络安全16 小时前
艾体宝干货 | Redis Java 开发系列#2 数据结构
java·数据结构·redis
sin_hielo16 小时前
leetcode 2211
数据结构·算法·leetcode