字符串函数

1.strlen

size_t  strlen( const char * str ); 

.字符串以‘\0’作为结束标志,strlen函数返回的个数是‘\0’之前的个数(不包含‘\0’)

.注意函数的返回值为size_t,是无符号的(易错)

.参数指向的字符串必须要以'\0'结束。

#include <stdio.h>
int main()
{
	char arr[100] = "abcd ef";
	int str = strlen(arr);
	printf("%d\n",str);
	return 0;
}

模拟实现:

#include <stdio.h>
size_t my_strlen(char* arr)
{
	char* start = arr;
	char* end = arr;
	while (*++end)
	{
		;
	}
	return end - start;
}
int main()
{
	char arr[100] = "abcd ef";
	int str = my_strlen(arr);
	printf("%d\n",str);
	return 0;
}

2.strcpy

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

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

#include <stdio.h>
#include <string.h>
int main()
{
	char arr[10] = { 0 };
	char* p = "abcde";
	strcpy(arr, p);
	printf("%s\n",arr);
	return 0;
}

#include <stdio>.h
size_t my_strcpy(char* arr,const char* arr1)
{
	while (*arr++ = *arr1++)
	{
		;
	}
	return arr;
}
int main()
{
	char arr[20] = "abc";
	char arr1[] = "hello bit";
	my_strcpy(arr, arr1);
	printf("%s\n",arr);
	return 0;
}

3.strcmp

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

Returns an integral value indicating the relationship between the strings:

return valueindicates
<0the first character that does not match has a lower value in ptr1 than in ptr2
0the contents of both strings are equal
>0the first character that does not match has a greater value in ptr1 than in ptr2
#include <stdio.h>
int main()
{
	char arr1[20] = "abcdef";
	char arr2[20] = "abcq";
	int ret = strcmp(arr1, arr2);
	printf("%d\n",ret);
	return 0;
}
#include <stdio.h>
int my_strcmp(char* arr1, const char* arr2)
{
	while (*arr1 ==*arr2)
	{
		if (*arr1 == '\0')
		{
			return 0;
		}
		arr1++;
		arr2++;
		
			
	}
	return *arr1 - *arr2;
}
int main()
{
	char arr1[20] = "abcdef";
	char arr2[20] = "abcq";
	int ret = my_strcmp(arr1, arr2);
	printf("%d\n",ret);//结果是d-q的差值
	return 0;
}

4.strcat

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

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

#include <stdio.h>
int main()
{
	char arr1[20] = "abcq";
	char arr2[20] = "abcq";
	int ret = strcat(arr1, arr2);
	printf("%s\n",arr1);
	return 0;
}
#include <stdio.h>
char* my_strcat(char* arr1, char* arr2)
{
	char* p = arr1;
	while (*p)
	{
		p++;
	}
	while (*p++= *arr2++)
	{
		;
	}
	return arr1;
}
int main()
{
	char arr1[20] = "abcq";
	char arr2[20] = "abcq";
	char* ret = my_strcat(arr1, arr2);
	printf("%s\n",ret);
	return 0;
}

 5.strstr

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

在一个集合里面找到子集。

#include <stdio.h>
char* my_strstr(const char* str1, const char* str2)
{
	const char* s1 = str1;
	const char* s2 = str2;
	const char* p = str1;
	if (*str2 == '\0')
	{
		return str1;
	}
	while (*p)
	{
		s1 = p;
		s2 = str2;
		while (*s1 != '\0' && *s2 != '\0' && (*s1 == *s2))
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return (char*)p;//找到了
		}
		p++;
	}
	return NULL;//找不到子串
}
int main()
{
	char arr1[20] = "abcqabc";
	char arr2[20] = "qa";
	char* ret = my_strstr(arr1, arr2);
	printf("%s\n",ret);
	return 0;
}

6.strtok

char * strtok (char * str , const char * sep);

.sep参数是个字符串,定义了用作分隔符的字符集合
.第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标
记。

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

#include <stdio.h>
#include <string.h>
int main()
{
	/*char a = "abced";
	printf("%s",a);*/
	char arr[] = "zhang@qq.com";
	char buff[20] = { 0 };
	strcpy(buff, arr);

	const char* p = "@.";
	char* str= strtok(buff, p);
	printf("%s\n",str);

	str=strtok(NULL, p);
	printf("%s\n",str);

	str=strtok(NULL, p);
	printf("%s\n", str);
	return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
	char arr[] = "zhang@qq.com";
	char buff[20] = { 0 };
	strcpy(buff, arr);
	const char* p = "@.";
	char* str = NULL;
	for (str = strtok(buff, p);str != NULL; str = strtok(NULL, p))
	{
		printf("%s\n",str);
	}
	return 0;
}

 7.memcpy

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

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

#include <stdio.h>
int main()
{
	int arr1[] = {1,2,3,4,5,6,7,8,9,10};
	int arr2[20] = {0};
	memcpy(arr2,arr1,20);
	
	return 0;
}

#include <stdio.h>
#include <assert.h>
void* my_memcpy(void* arr2, void* arr1, size_t num)
{
	void* ret = arr2;
	assert(arr1 && arr2);
	while (num--)
	{
		*(char*)arr2 = *(char*)arr1;
		arr1 = (char*)arr1 + 1;
		arr2 = (char*)arr2 + 1;
	}
	return ret;
}
int main()
{
	int arr1[] = {1,2,3,4,5,6,7,8,9,10};
	int arr2[20] = { 0 };
	my_memcpy(arr2,arr1,20);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ",arr2[i]);
	}
	return 0;
}

 8.memmove

void * memmove ( void* destination, constvoid* source, size_tnum);

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

#include <stdio.h>
int main()
{
	int arr1[] = {1,2,3,4,5,6,7,8,9,10};
	int arr2[20] = {0};
	memmove(arr1+2, arr1, 16);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ",arr1[i]);
	}
	return 0;
}

 

#include <stdio.h>
#include <assert.h>
void* my_memmove(void* des,void* res,size_t num)
{
	void* ret = des;
	assert(des&&res);
	if (des < res)
	{
		while (num--)
		{
			*(char*)des = *(char*)res;
			des = (char*)des + 1;
			res = (char*)res + 1;
		}
	}
	else
	{
		while (num--)
		{
			*((char*)des + num) = *((char*)res + num);
		}
	}
	return ret;
}
int main()
{
	int arr1[] = {1,2,3,4,5,6,7,8,9,10};
	int arr2[20] = {0};
	my_memmove(arr1+2, arr1, 16);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ",arr1[i]);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值