C语言复习巩固知识点_04_内存四区举例&指针字符串逆转

博客主要介绍了指针逆转字符串相关内容,重点提及俩头绪模型。

 

指针逆转字符串:

int inverse(char *buf)
{
	if (buf == NULL)
	{
		printf("buf==NULL");
		return -1;
	}
	int length = strlen(buf);
	char *p1 = buf;
	char *p2 = buf + length - 1;

	while (p1 < p2)
	{
		char c = *p1;
		*p1 = *p2;
		*p2 = c;
		p1++;
		p2--;
	}
	return 0;
}
int inverse02(char *p)
{
	if (p == NULL)
	{
		return -1;
	}
	if (*p == '\0')
	{
		return 0;
	}
	inverse02(p + 1);
	printf("%c", *p);

}
int inverse03(char *p)
{
	if (p == NULL)
	{
		return;
	}
	if (*p == '\0')
	{
		return;
	}
	inverse03(p + 1);
	//printf("%c", *p);
	strncat(g_buf, p, 1);

}
int inverse04(char *p,char *mybuf)
{
	if (p == NULL||mybuf==NULL)
	{
		return;
	}
	if (*p == '\0')
	{
		return;
	}
	inverse04(p + 1,mybuf);
	//printf("%c", *p);
	strncat(mybuf, p, 1);

}

俩头绪模型

int getCount(char *p, int *count)
{
	if (p == NULL || count == NULL)
	{
		printf("p == NULL || count == NULL");
		return -1;
	}
	int i = 0;
	int j = strlen(p) - 1;
	while (isspace(p[i]) && p[i] != '\0')
	{
		i++;
	}
	while (isspace(p[j]) && p[j] != '\0')
	{
		j--;
	}
	int AllCount = j - i + 1;
	*count = AllCount;
	return 0;
}
int trimSpace(char *p, char *newstr)
{
	if (p == NULL || newstr == NULL)
	{
		printf("p == NULL || count == NULL");
		return -1;
	}
	int i = 0;
	int j = strlen(p) - 1;
	while (isspace(p[i]) && p[i] != '\0')
	{
		i++;
	}
	while (isspace(p[j]) && p[j] != '\0')
	{
		j--;
	}
	int AllCount = j - i + 1;
	p = p + i;
	strncpy(newstr, p, AllCount);
	newstr[AllCount] = '\0';
	return 0;
}
int trimSpace1(char *p)
{
	if (p == NULL)
	{
		printf("p == NULL || count == NULL");
		return -1;
	}
	int i = 0;
	int j = strlen(p) - 1;
	while (isspace(p[i]) && p[i] != '\0')
	{
		i++;
	}
	while (isspace(p[j]) && p[j] != '\0')
	{
		j--;
	}
	int AllCount = j - i + 1;
	//p = p + i;
	strncpy(p, p+i, AllCount);
	p[AllCount] = '\0';
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值