数据结构预科

在堆区申请两个长度为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;
}
相关推荐
by2099916 小时前
哈希表——快速找到数据:冲突、删除与扩容的实现
数据结构·c++·哈希算法·散列表
Code_Solitude17 小时前
c语言:关于qsoet函数知识点
c语言·数据结构·算法
Pniubi18 小时前
快速排序(力扣基础)(递归)
java·数据结构·算法
inrichtt1 天前
第八课时:SPI总线揭秘:四线全双工通信的奥
c语言·数据结构·stm32·单片机·嵌入式硬件
HPtiger1 天前
数据结构——选择排序
数据结构·算法·排序算法
丑梨数据1 天前
信创环境里跑文档工具:格式兼容与本地合规的几个坑
数据结构·人工智能·算法·决策树·排序算法
烂蜻蜓1 天前
Pandas入门教程(四):Series数据结构——创建、索引与常用方法
数据结构·pandas
CS创新实验室2 天前
一棵树怎么做到“自平衡还不太费力“?——红黑树的旋转与染色
数据结构·算法·红黑树·408·计算机考研
Logic1012 天前
C语言/数据结构位运算题解:异或XOR找出被挑选一次的果子编号——只出现一次的数字
c语言·数据结构·算法·数组·位运算·时间复杂度·空间复杂度
从小白到软工2 天前
4-3 项目-学生成绩管理系统(上)
c语言·开发语言·数据结构·经验分享·ai编程