初学结构体,分文件编写

1.在堆区申请2个字符类型的大小为20字节的空间。

1> 定义函数,实现在堆区申请空间

2> 定义函数,输入两个字符串

3> 定义函数,计算两个字符串的长度【非函数】

sizeof_t my_strlen(const char *s1) //注意:sizeof_t是unsigned int的别名

4> 定义函数,实现字符串连接

char *my_strcat(const char *dest,const char *src)

5> 定义函数,实现连接后字符串的冒泡排序【是对字符串的每一个字符进行排序】

void Bubble(char *s)

6> 实现字符串逆置

7> 调用函数释放空间

head.h

#ifndef N
#define N
#define sizeof_t unsigned int

#include<stdio.h>
#include<stdlib.h>

char *create(int n);
void input(char *p);
//sizeof_t my_strlen(const char *s1);
char *my_strcat(const char *s2,const char *s3);
void bubble(char *s);
void daoxu(char *s4);
	
char *free_space(char *p);

#endif

test.c

#include"head.h"

char *create(int n)
{
	char *p=(char *)malloc(n);
	if(p==NULL)
		return NULL;
	return p;
}


void input(char *p)
{
	scanf("%s",p);
}

//字符串长度
sizeof_t my_strlen(const char *s1)
{
	int i;
	for (i = 0; s1[i] != '\0'; i++);
	return i;
}

//字符串连接
char *my_strcat(const char *dest,const char *src)
{
  char *p = dest +my_strlen(dest);
  while(*p++ = *src++);
  return dest;
}

//冒泡排序
void bubble(char *s)
{
	char *p = NULL;
	p = s;
	int count = 0;
	while (*p != '\0')
	{
		p++;
		count++;
	}
	for (int i = 0; i < count-1; i++)
		for (int j = 0; j < count-1-i; j++)
			if (s[j] > s[j + 1])
			{
				int temp = s[j];
				s[j] = s[j + 1];
				s[j + 1] = temp;
			}
}

//倒序输出
void daoxu(char *s4)
{
	char *s = NULL;
	s = s4;
	int count = 0;
	while (*s != '\0')
	{
		s++;
		count++;
	}
		int i = 0;
		int j = count - 1;
		while(i<j)
		{
			int temp = s4[i];
			s4[i] = s4[j];
			s4[j] = temp;
			i++;
			j--;
		}
}

//释放空间
char *free_space(char *p)
{
    if(p==NULL)
        return NULL;
    free(p);
    p=NULL;
    return p;
}

main.c

#include<stdio.h>
#include"head.h"


int main(int argc, const char *argv[])
{
	
	char *a = create(20);
	char *b = create(20);
	printf("输入第一个字符串:");
	input(a);
	printf("输入第二个字符串:");
	input(b);
	int m = my_strlen(a);
	printf("输入第一个字符串长度为:%d\n",m);

	int n = my_strlen(b);
	printf("输入第二个字符串长度为:%d\n",n);


	my_strcat(a,b);
	printf("字符串a,b连接后:%s\n",a,b);


	bubble(a);
	printf("第一个字符串冒泡排序后的结果为:%s\n",a);
	
	daoxu(b);
	printf("b字符串倒序输出为:%s\n",b);

	a = free_space(a);
	b = free_space(b);	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值