字符函数和字符串函数

目录

1.函数介绍

1.1  strlen

1.2  strcpy

1.3  strcat

1.4  strcmp

1.5  strncpy

1.6  strncat

1.7  strncmp

1.8  strstr

1.9  strtok

1.10  strerror

1.11  memcpy

1.12  memmove

1.13  memcmp

2.库函数的模拟实现

2.1  模拟实现strlen

2.2  模拟实现strcpy

2.3  模拟实现strcat

2.4  模拟实现strstr

2.5  模拟实现strcmp

2.6  模拟实现memcpy

2.7  模拟实现memmove


1.函数介绍

1.1  strlen

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

注:

1.2  strcpy

  •  Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
  • 源字符串必须以'\0'结束。
  • 会将源字符串中的'\0'拷贝到目标空间。
  • 目标空间必须足够大。以确保能存放源字符串。
  • 目标空间必须可修改。

1.3  strcat

  • Appends a copy of the source string to the destination string.The terminating null chaaracter in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
  • 源字符串必须以'\0'结束。
  • 目标空间必须足够大。以确保能存放源字符串。
  • 目标空间必须可修改。

1.4  strcmp

  • This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the chaaracter differ or until a terminating null-character is reached

标准规定:

  • 第一个字符串大于第二个字符串,则返回大于0的数字
  • 第一个字符串等于第二个字符串,则返回0
  • 第一个字符串小于第二个字符串,则返回小于0的数字

1.5  strncpy

  • Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it. 
  • 拷贝num个字符从源字符串到目标空间。
  • 如果源字符串的长度小于num,则拷贝完源字符串后,在目标的后面追加0,直到num个。

1.6  strncat

  • Appends the first num characters of source to destination, plus a terminating null_character.
  • If the length of the C string in source is less than num, only the content up to the terminating null-character is copied. 

1.7  strncmp

  •  比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。 

1.8  strstr

  • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

 

1.9  strtok

  • sep参数是个字符串,定义了用做分隔符的字符集合
  • 第一个参数指定一个字符串,它包含了0个或多个由sep字符串中一个或多个分隔符分割的标记。
  • strtok函数找到str中的下一个标记,并将其用'\0'结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
  • strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
  • strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
  • 如果字符串中不存在更多的标记,则返回NULL指针。

 

1.10  strerror

 

字符分类函数:

字符转换:

 

1.11  memcpy

  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
  • 这个函数在遇到'\0'的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。

1.12  memmove

  • 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就得用memmove函数处理。 

1.13  memcmp

  • 比较从ptr1和ptr2指针开始的num个字节 

  

2.库函数的模拟实现

2.1  模拟实现strlen

int my_strlen(char* p)
{
	int count = 0;
	while (*p++ != '\0')
	{
		count++;
	}

	return count;
}

int main()
{
	char arr[] = "孙悟空";
	printf("%d\n", my_strlen(arr));
	
	return 0;
}

2.2  模拟实现strcpy

char* my_strcpy(char* dest, const char* sour)
{
	assert(dest && sour);

	while (*dest++ = *sour++);

	return dest;
}

int main()
{
	char arr1[20];
	char arr2[] = "孙悟空";
	my_strcpy(arr1, arr2);
	puts(arr1);

	return 0;
}

2.3  模拟实现strcat

char* my_strcat(char* dest, const char* sour)
{
	assert(dest && sour);

	char* p = dest;
	while (*dest != '\0')
	{
		dest++;
	}
	

	while (*dest++ = *sour++);


	return p;
}

int main()
{
	char arr1[10] = "abc";
	char arr2[] = "def";
	my_strcat(arr1, arr2);

	printf("%s\n", arr1);

	return 0;
}

2.4  模拟实现strstr

char* my_strstr(const char* dest, const char* sour)
{
	assert(dest && sour);

	char* begin = dest;

	while (*begin)
	{
		char* p1 = begin;
		char* p2 = sour;

		while (*p1 == *p2 && *p2)
		{
			p1++;
			p2++;
		}
		if (!*p2)
		{
			return begin;
		}

		begin++;
	}

	return 0;
}

int main()
{
	char arr1[] = "abbbcd";
	char arr2[] = "bcd";

	if (my_strstr(arr1, arr2))
	{
		printf("Yes\n");
	}
	else
	{
		printf("No");
	}

	return 0;
}

2.5  模拟实现strcmp

int my_strcmp(const char* p1, const char* p2)
{
	assert(p1 && p2);

	while (*p1++ == *p2++);

	return *p1 - *p2;
}

int main()
{
	char arr1[] = "abc";
	char arr2[] = "abcdef";

	printf("%d\n", my_strcmp(arr1, arr2));

	return 0;
}

2.6  模拟实现memcpy

void* my_memcpy(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;

	while (num--)
	{
		*(char*)dest = *(char*)src;

		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}

	return ret;
}


int main()
{
	char a[] = "hello bit!";
	char b[20] = { 0 };

	my_memcpy(b, a, sizeof(a));
	puts(b);

	return 0;
}

2.7  模拟实现memmove

void* my_memmove(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;

	if (dest < src)
	{
		while (num--)
		{
			*(char*)dest = *(char*)src;

			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		while (num--)
		{
			*((char*)dest + num) = *((char*)src + num);
		}
	}

	return ret;
}


int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9 };

	my_memmove(arr+2, arr, 20);
	for (int i = 0; i < 9; i++)
	{
		printf("%d ", arr[i]);
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值