数据结构预科

在堆区申请两个长度为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;
}
相关推荐
Fanxt_Ja1 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1231 天前
【数据结构】二叉树的概念
数据结构·二叉树
散1122 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19432 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww2 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚2 天前
[数据结构] 排序
数据结构
睡不醒的kun2 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌2 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long2 天前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn2 天前
Redis7底层数据结构解析
前端·数据结构·bootstrap