数据结构预科

在堆区申请两个长度为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;
}
相关推荐
2401_8582861114 分钟前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
双叶8361 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
学不动CV了4 小时前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
算法_小学生6 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
Wo3Shi4七9 小时前
哈希冲突
数据结构·算法·go
V我五十买鸡腿10 小时前
顺序栈和链式栈
c语言·数据结构·笔记·算法
七灵微11 小时前
数据结构实验习题
数据结构
杰克尼21 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
xiaolang_8616_wjl1 天前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
hqxstudying1 天前
Java创建型模式---单例模式
java·数据结构·设计模式·代码规范