字符函数和内存函数

一、字符函数

1、strlen(求字符串长度) 

用法:

size_t strlen (const char * str);

Returns the length of the C string str.

(1)字符串把‘\0’作为结束标志,strlen函数返回的是在字符串中‘\0’前面出现的字符个数(不包括‘\0’)。

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

(3)注意函数的返回值为size_t,是无符号的。

函数模拟实现

#include<stdio.h>
#include<string.h>
size_t my_strlen(const char* str)
{
	int count = 0;
	while (*str != '\0')
	{
		str++;
		count++;
	}
	return count;
}
int main()
{
	char arr[] = "abcdefg";
	size_t len = my_strlen(arr);
	printf("%zd\n", len);
	return 0;
}

2、strcpy(字符串的拷贝) 

用法:

char* strcpy ( char * destination , const char * source );
Copies the C string pointed by source into the array pointed by destination, including the
terminating null character (and stopping at that point).
(1)源字符串必须以 '\0' 结束。
(2)会将源字符串中的 '\0' 拷贝到目标空间。
(3)目标空间必须足够大,以确保能存放源字符串。
(4)目标空间必须可变。
函数模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* dest, const char* scr)
{
	char* ret = dest;
	assert(dest);//断言,谨防空指针;
	assert(scr);
	while (*dest=*scr)
	{
		dest++;
		scr++;
	}
	return ret;
}
int main()
{
	char arr1[] = "***************************";
	char arr2[] = "abcdefg";
	my_strcpy(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

3、strcat(把后一个字符串追加到前一个字符串上)

用法:

char * strcat ( char * destination , const char * source );
Appends a copy of the source string to the destination string. The terminating null character
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.
(1)源字符串必须以 '\0' 结束。
(2)目标空间必须有足够的大,能容纳下源字符串的内容。
(3)目标空间必须可修改。
函数模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest && src);
	while (*dest != '\0')
	{
		dest++;
	}
	while (*dest++ = *src++)
	{
		;
	}
	return ret;//返回目标函数的起始地址;
}
int main()
{
	char arr1[20] = "abc";
	char arr2[] = "def";
	my_strcat(arr1, arr2);
	printf("%s\n", arr1);
	return 0;
}

4、strcmp(比较两个字符串的大小,按照字符的ASCII值比较)

用法:int strcmp ( const char * str1, const char * str2 );

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 characters differ or until a terminating
null-character is reached.
标准规定:
(1)第一个字符串大于第二个字符串,则返回大于 0 的数字
(2)第一个字符串等于第二个字符串,则返回 0
(3)第一个字符串小于第二个字符串,则返回小于 0 的数字
函数模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
			return 0;
		str1++;
		str2++;
	}
	if (*str1 > *str2)
	{
		return 1;
	}
	else
		return -1;
}
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abce";
	if (my_strcmp(arr1, arr2) > 0)
		printf(">\n");
	else
		printf("<=\n");
	return 0;
}

5、strncat  strncpy strncmp与上面 strcat strcpy strcmp用法完全一致,只不过参数需要在后面加上需要追加、拷贝、比较的字符个数

6、strstr(在字符串中找字符串)

用法:char * strstr ( const char *str1, const char * str2)。

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of
str1.
函数模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
const char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	if (*str2 == '\0')        //如果要找的是空字符串,直接输出目标字符串;
		return str1;
	const char* cp;
	const char* s1;
	const char* s2;
	cp = str1;   
	while (*cp != '\0')
	{
		s1 = cp;//s1遍历第一个字符串
		s2 = str2; //S2遍历第二个字符串;
		while (*s1 && *s2 && *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
			return cp;
		cp++;
	}
	return NULL;
}
int main()
{
	char arr1[] = "abbcdef";
	char arr2[] = "bc";
	const char *ret=my_strstr(arr1, arr2);
	if (ret == NULL)
	{
		printf("找不到\n");
	}
	else
	printf("%s\n", ret);
	
	return 0;
}

7、strtok(切割字符串)

用法:

char * strtok ( char * str , const char * sep );
(1)sep 参数是个字符串,定义了用作分隔符的字符集合
(2)第一个参数指定一个字符串,它包含了 0 个或者多个由 sep 字符串中一个或者多个分隔符分割的标 记。
(3)strtok 函数找到 str 中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:
strtok 函数会改变被操作的字符串,所以在使用 strtok 函数切分的字符串一般都是临时拷贝的内容
并且可修改。)
(4)strtok 函数的第一个参数不为 NULL ,函数将找到 str 中第一个标记, strtok 函数将保存它在字符串中的位置。
(5)strtok 函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标
记。
(6)如果字符串中不存在更多的标记,则返回 NULL 指针。
#include<stdio.h>
#include<string.h>
int main()
{
	char arr[] = "[email protected]";
	char* p = "@.";
	char buf[100] = { 0 };
	strcpy(buf, arr);
	char* s = NULL;
	for (s = strtok(buf, p); s != NULL; s = strtok(NULL, p))
	{
		printf("%s\n", s);
	}
	return 0;
}

二、内存函数

1、memcpy(针对不重叠的内存拷贝)

用法:

void * memcpy ( void * destination , const void * source , size_t num );
(1)函数 memcpy source 的位置开始向后复制 num 个字节的数据到 destination 的内存位置。
(2)这个函数在遇到 '\0' 的时候并不会停下来。
(3)如果 source destination 有任何的重叠,复制的结果都是未定义的。
模拟实现
#include<stdio.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t sz)
{
	assert(dest && src);
	void* ret = dest;
	while (sz--)
	{
		*(char*)dest = *(char*) src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}
int main()
{
	int arr1[20] = { 0 };
	int arr2[] = { 1,2,3,4,5 };
	my_memcpy(arr1, arr2,20);//20是20个字节
	int i = 0;
	for (i = 0; i < 5; i++)
		{
			printf("%d ", arr1[i]);
		}
	return 0;
}

2、memmove(拷贝重叠的内存)

用法:

void * memmove ( void * destination , const void * source , size_t num );
(1)和 memcpy 的差别就是 memmove 函数处理的源内存块和目标内存块是可以重叠的。
(2)如果源空间和目标空间出现重叠,就得使用 memmove 函数处理。
模拟实现
#include<stdio.h>
#include<assert.h>
void* my_memmove(void* dest, void* src, size_t sz)
{
	assert(dest && src);
	void* ret = dest;
	if (src > dest)//从前往后覆盖
	{
		while (sz--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else//从后往前覆盖;
	{
		while (sz--)
		{
			*((char*)dest + sz) = *((char*)src + sz);
		}
	}
	return ret;
}
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr+2, arr , 20);
	int i = 0;
	for (i = 0; i < (sizeof(arr) / sizeof(arr[0])); i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

3、memcmp(比较指定内存的字节个数大小)

用法:

int memcmp ( const void * ptr1 , const void * ptr2 , size_t num );
#include<stdio.h>
#include<string.h>
int main()
{
	char buffer1[] = "DWgaOtP12df0";
	char buffer2[] = "DWGAOTP12DF0";
	int n;
	n = memcmp(buffer1, buffer2, sizeof(buffer1));
	if (n > 0) 
		printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
	else if (n < 0) 
		printf("'%s' is less than '%s'.\n", buffer1, buffer2);
	else 
		printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_67131528/article/details/133754512