数据结构预科

在堆区申请两个长度为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;
}
相关推荐
code斗7 小时前
Java数据结构:堆详解
java·开发语言·数据结构
Elaine33610 小时前
数据结构与算法-程序
数据结构·python·算法·计算机基础·编程基础
Logic10111 小时前
C语言/数据结构位运算题解:异或XOR找出游戏速通中的“独特初始能量值“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
Bruce_Liuxiaowei13 小时前
Python 字典默认值完全指南:从 KeyError 到 defaultdict
开发语言·数据结构·python·字典
All for pursuit.17 小时前
【二叉树-10】114.二叉树展开为链表
数据结构·c++·算法·leetcode·链表
心中有你021419 小时前
C语言埃氏筛法(素数筛选)
c语言·数据结构·算法
释厄62321 小时前
多黑洞具现论·物理/生物/金融三域显化·虚拟宇宙黑洞·奇点=0 圆圈
开发语言·数据结构·人工智能·程序人生·安全威胁分析
库玛西1 天前
攻克 408 数据结构:图论基石深度拆解(数学极值推演 + 存储内存剖析 + BFS/DFS 双核模板)
数据结构·算法·深度优先·广度优先·图搜索算法
m0_547486667 天前
《数据结构教程》全套 PPT课件2026
数据结构
tryxr7 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵