Day08,Day09 指针(下)

Day08,Day09 指针

十二、指针和函数

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

//地址传递,可以改变实参的值
void tab(int* a, int* b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
	return;
}

int main(void)
{
	int a = 10;
	int b = 20;
	tab(&a, &b);
	printf("%d    %d\n", a, b);


	return 0;
}

十三、指针作为函数参数

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

void print(int* arr,int len)
{
	//函数参数中如果有数组,都会转化为指针  sizeof(int *) 4   所以求出来的值不能作为循环条件
	//printf("%d\n", sizeof(arr));
	for (int i = 0; i < len; i++)
	{
		printf("%d\n", *(arr + i));
	}
	return;
}

int main(void)
{
	int arr[] = { 1,2,5,3,9,6,8,7,4,0 };
	int len = sizeof(arr) / sizeof(arr[0]);
	print(arr,len);

	//printf("%d\n", sizeof(arr));

	return 0;
}

十四、函数的返回值是指针

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

char* test()
{
	//字符数组 创建位置在栈区
	//char arr[] = "hello world!";
	
	//字符串常量 会在程序运行时 放在常量区 不能修改  在程序结束时销毁
	char* arr = "hello world!";
	
	return arr;
}

int main(void)
{
	char* a = test();
	printf("%s\n", a);
	
	return 0;
}

十五、通过数组形式,实现strstr函数

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

/*
1、两个匹配的字符串,必须完全匹配  (匹配个数 = 字符串长度)
2、如果匹配一个字符串,需要记录被匹配字符串地址
3、如果匹配一半未成功,回到记录被匹配字符串地址+1
*/
char* mystrstr(char* dest, char* src)
{
	char* p = NULL;
	int i = 0;
	int j = 0;
	int count = 0;
	int len = strlen(src);
	while (dest[i] != 0)
	{
		while (dest[i] == src[j])
		{
			if (!count)
			{
				p = &dest[i];
			}
			count++;
			i++;
			j++;
			if (count == len)
			{
				return p;
			}
		}
		i = i - count+1; //hellllo world     llo   i=4 j=2 count=2
		count = 0;
		j = 0;	
	}
	return NULL;

}

int main(void)
{
	char arr[] = "helllollll worldllo!";
	char arr1[] = "lo";
	char* p = mystrstr(arr, arr1);
	printf("%s\n", p);


	return 0;
}

十六、通过数组形式,实现strstr函数(优化)

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

/*
1、两个匹配的字符串,必须完全匹配  (匹配个数 = 字符串长度)
2、如果匹配一个字符串,需要记录被匹配字符串地址
3、如果匹配一半未成功,回到记录被匹配字符串地址+1
*/
char* mystrstr(char* dest, char* src)
{
	char* p = NULL;
	int i = 0;
	int j = 0;
	int count = 0;
	int len = strlen(src);
	while (dest[i] != 0)
	{
		p = &dest[i];
		while (dest[i] == src[j])
		{
			count++;
			i++;
			j++;
		}
		if (count == len)
		{
			return p;
		}
		i = i - count + 1; //hellllo world     llo   i=4 j=2 count=2
		count = 0;
		j = 0;
	}
	return NULL;

}

int main(void)
{
	char arr[] = "helllollll worldllo!";
	char arr1[] = "lo";
	char* p = mystrstr(arr, arr1);
	printf("%s\n", p);


	return 0;
}

十七、通过指针形式,实现strstr函数

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

/*
1、两个匹配的字符串,必须完全匹配  (匹配个数 = 字符串长度)
2、如果匹配一个字符串,需要记录被匹配字符串地址
3、如果匹配一半未成功,回到记录被匹配字符串地址+1
*/
char* mystrstr(char* dest, char* src)
{
	char* p = NULL;
	char* temp = src;
	while (*dest)
	{
		p = dest;
		while (*dest == *temp)
		{
			temp++;
			dest++;
		}
		if (!*temp)
			return p;
		dest = p;
		dest++;
		temp = src;

	}
	return NULL;

}

int main(void)
{
	char arr[] = "helllollll worldllo!";
	char arr1[] = "dll";
	char* p = mystrstr(arr, arr1);
	printf("%s\n", p);


	return 0;
}

十八、字符串排序

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


//字符串排序  根据字符串首字母 按照a-z的顺序排序
//student tree new bee

//用二级指针来实现
void bubble1(char** arr, int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (**(arr + j) > **(arr + j + 1))
			{
				char* temp = *(arr + j);
				*(arr + j) = *(arr + j + 1);
				*(arr + j + 1) = temp;
			}
		}
	}
}

//用二维数组来实现
void bubble2(char** arr, int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (arr[j][0] > arr[j+1][0])
			{
				char* temp = arr[j];
				arr[j] = arr[j+1];
				arr[j + 1] = temp;
			}
		}
	}
}

//用一半指针一般数组实现
void bubble3(char** arr, int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (*arr[j] > *arr[j+1])
			{
				char* temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
int main(void)
{
	char* arr[] = { "student","tree","new","bee" };
	int len = sizeof(arr)/sizeof(*arr);
	bubble1(arr, len);
	//bubble2(arr, len);
	//bubble3(arr, len);
	for (int i = 0; i < len; i++)
	{
		printf("%s\n", *(arr + i));
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值