欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > 数据结构预科

数据结构预科

2024/10/23 23:34:12 来源:https://blog.csdn.net/m0_75102741/article/details/140108168  浏览:    关键词:数据结构预科

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

要求:

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

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

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

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

4> 定义函数,释放空间

 

#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;
}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com