部分字符串函数的介绍及实现

部分字符串函数的介绍及实现

1.1strlen(字符串求长度函数)的介绍

size_t strlen ( const char * str );

字符串已经 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包含 ‘\0’ )。
参数指向的字符串必须要以 ‘\0’ 结束。
注意函数的返回值为size_t,是无符号的。

1.2strlen的实现

#include<stdio.h>
size_t mystrlen(const char * str)//返回值为无符号数
{
	size_t i = 0;
	while (*str++)//遍历字符串直到‘\0’
	{
		i++;
	}
	return i;
}
int main()
{
	char p[] = "agiyccb";
	size_t l;
	l = mystrlen(p);
	printf("%u\n", l);
	return 0;
}

2.1strcpy(字符串拷贝函数)的介绍

char* strcpy(char * destination, const char * source );

从source中拷贝到destination中
源字符串必须以 ‘\0’ 结束。
会将源字符串中的 ‘\0’ 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。
目标空间必须可变。

2.2strcpy的实现

#include<stdio.h>
char* mystrcpy(char * destination, const char * source)
{
	char* a = destination;
	assert(destination&&source);
	while (*destination++ = *source++)//通过while循环不断赋值,直到将'\0'赋值后循环结束
	{
		;
	}
	return a;
}
int main()
{
	char a[100] = "abj";
	char k[] = "acohaoc";
	char*o = mystrcpy(a, k);
	printf(o);
	return 0;
}

在这里我们拷贝字符串的时候只要在source中遇到’\0’时拷贝就会停止。

3.1strcmp(字符产比较函数)的介绍

int strcmp ( const char * str1, const char * str2 );

将str1的内容与str2的进行比较
如果str1大于str2则返回一个大于0的数字,若str1的内容小于str2的就返回一个小于0的数字,若二者相等则返回0。

3.2strcmp的实现

#include<stdio.h>
int mystrcmp(const char * str1, const char * str2)//字符串比较函数
{
	assert(str1&&str2);
	while (*str1)//当其为'\0'时结束
	{
		if (*str1 == *str2)
		{
			str1++;
			str2++;
		}
		else
			return *str1 - *str2;//若有一组不相等则返回二者相减刚好可以满足函数要求
	}
	return 0;//此时两个字符串都相等则返回0.
}
int main()
{
	char a[] = "abc";
	char b[] = "abdd";
	int c = mystrcmp(a, b);
	if (c > 0)
	{
		printf("a大\n");
	}
	else if (c < 0)
	{
		printf("b大\n");
	}
	else
	{
		printf("=\n");
	}
	return 0;
}

4.1strcat(字符串追加函数)的介绍

char * strcat ( char * destination, const char * source );

源字符串必须以 ‘\0’ 结束。
目标空间必须有足够的大,能容纳下源字符串的内容。目标空间必须可修改。

4.2strcat的实现

#include<stdio.h>
char *mystrcat(char *dest, const char*src)
{
	char *ret = dest;
	assert(dest&&src);
	while (*dest)//找到dest串的'\0'位置
	{
		dest++;
	}
	while ((*dest++ = *src++))//将后面的内容通过循环赋值
	{
		;
	}
	return ret;
}
int main()
{
	char a[20] = "abc";
	char b[] = "bui";
	char* c = mystrcat(a, a);
	printf("%s\n", c);
	return 0;
}

5.1strstr(字符串查找函数)介绍

char * strstr ( const char *str2, const char * str1);

在str2中寻找str1.找到后返回其在str2中第一次出现的位置,否则返回NULL

5.2strstr函数的实现

char * mystrstr(const char *str2, const char * str1)
{
	assert(str1&&str2);
	char* x = str1;
	char* y = str2;
	while (*y)//*y若被查找的字符串直到其遍历完都没找到那么就退出
	{
		char* o = y;//使y指向第一次开始查找的下一位置
		while (*y!=0&&*x!=0&&*x == *y)
		{
			x++;
			y++;
		}
		if (*x == 0)
		{
			return o;
		}
		x = str1;
		y = o+1;//不能写为o++,否则会使程序死循环。
	}
	return NULL;
}
int main()
{
	char l[] = "adeydwdg";
	char t[] = "wdg";
	char* u = mystrstr(l, t);
	if (u != NULL)
	{
		printf("找到了\n");
		printf("%s", u);
	}
	else
	{
		printf("找不到\n");
	}

	return 0;
}

6.1memcpy(内存拷贝函数)的介绍

void * memcpy ( void * destination, const void * source, size_t num );

函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
source和destination有任何的重叠会导致复制达不到想要的效果。

6.2memcpy的实现

#include<stdio.h>
void * mymemcpy(void * destination, const void * source, size_t num)//这里为void*是为了兼容各种类型的数据
{
	void* d = destination;
    assert(destination&&source);
	while (num--)
	{
		*(((char*)destination)++) = *(((char*)source)++);//将指针强制类型转换为char*后进行赋值
	}
	return d;
}

int main()
{
	int a[] = { 0, 1, 2, 3, 4 };
	int b[] = { 8, 9, 10 };
	void* n = mymemcpy(a, b, 12);
	return 0;
	}

7.1memmove(内存拷贝函数)的介绍

void * memmove ( void * destination, const void * source, size_t num );

他和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。在重叠时使用memmove

7.2memmove的实现

void* mymemmove(void*goal, const void*source, size_t count)
{
	void* a = goal;
	assert(goal&&source);
	if (goal < source)
	{
		while (count--)
		{
			*(char*)goal = *(char*)source;
			((char*)source)++;
			((char*)goal)++;
		}
	}
	else
	{
		while (count--)
		{
			*((char*)goal + count) = *((char*)source + count);
		}
	}
	return a;
}
int main()
{
	int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	mymemmove(arr,arr+5,20);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值