作业作业 作业

1、使用指针实现 strcpy、strcat 函数的功能

#include <stdio.h>

int main(int argc, const char *argv[])
{
	char s1[32] = "aabbcc";
	char s2[32] = "112233";
	char *p1 = &s1;
	char *p2 = &s2;

	while(*p1)
	{
		p1++;
	}
	while(*p2)
	{
		*p1 = *p2;
		p1++;
		p2++;
	}
	*p1 = *p2;//'\0'
	p1 = s1;//重新让指针指向字符串首地址
	p2 = s2;
	printf("%s\n",p1);
	printf("%s\n",p2);

	return 0;
}
#include <stdio.h>

int main(int argc, const char *argv[])
{
	char s1[32] = "helloqwer";
	char s2[32] = "world";
	
	char *p1 = s1;
	char *p2 = s2;

	while(*p2)
	{
		*p1++=*p2++;
	}
	*p1 = *p2;
	p1 = s1;//重新让指针指向字符串首地址
	p2 = s2;
	printf("%s\n",p1);
	printf("%s\n",p2);
	return 0;
}


2、设计一个程序,判断操作系统是大端存储还是小端存储

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int a = 0x12345678;
	char *p = &a;

	if(*p=0x78)
	{
		printf("小\n");
	}
	else
	{
		printf("大\n");
	}
	return 0;
}


3、指针实现冒泡排序

#include <stdio.h>

int main(int argc, const char *argv[])
{
	int arr[5]={8,9,4,2,5};
	int *p = arr;
	int i = 0;
	int j = 0;
	int temp = 0;

	for(i=1;i<5;i++)
	{
		for(j=0;j<5-i;j++)
		{
			if(p[j]>p[j+1])
	
			{
				temp = p[j+1];
				p[j+1] = p[j];
				p[j] = temp;
				
			}
		}
	}
	for(i=0;i<5;i++)
	{
		printf("%d\n",p[i]);
	}

	return 0;
}


4、用指针的方式进行字符串倒叙。"abcdef" ==> "fedcba"

#include <stdio.h>

int main(int argc, const char *argv[])
{
	char s1[]="abcdef";
	char *p = s1;
	int count = 0;
	int temp = 0;

	while(*p)
	{
		count++;
		p++;
	}
	printf("%d\n",count);
	for(int k=0;k<count/2;k++)
	{
		temp = s1[k];
		s1[k]=s1[count-k-1];
		s1[count-k-1]=temp;
	}
	puts(s1);
	return 0;
}


5、终端获取字符串删除空格,用指针方式实现
 

#include <stdio.h>
int main(int argc, const char *argv[])
{
	char s1[30]="";
	gets(s1);
	char *p=s1;
	int i=0,j=0;
	while (p[i]!='\0')
	{
		if (p[i]!=' ')
		{
			p[j] = p[i];
			j++;
		}
		i++;
	}
	p[j] = '\0';
	puts(s1);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值