字符函数和字符串函数(二)

长度受限制的字符串函数:
strncpy
strnzat
strncmp

1.5 strncpy

char * strncpy ( char * destination, const char * source, size_t num );

  • 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个。
#include <stdio.h>
int main()
{
	char arr1[20] = "abcdef";
	char arr2[] = "hello";
	strncpy(arr1, arr2, 5);
	//传递参数分别为:目标函数,源函数,传递的参数的个数
	char arr3[] = "xyz";
	strncpy(arr1, arr3, 5);
	//当传递参数的个数大于源函数中元素的个数时,在目标函数后追加0
	printf("%s", arr1);
	return 0;
}

1.6 strncat

char * strncat ( char * destination, const char * source, size_t num );

  • 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.
#include <stdio.h>
int main()
{
	char arr1[20] = "abcdef";
	char arr2[] = "hello";
	strncat(arr1, arr2, 5);
	//strncat函数会在追加完后自动补一个'\0'
	char arr3[] = "xyz";
	strncat(arr1, arr3, 5);
	//当源函数的参数小于传递参数的个数时,后面只补一个'\0'
	printf("%s\n", arr1);
	return 0;
}

1.7 strncmp

int strncmp ( const char * str1, const char * str2, size_t num );

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

标准规定:

  • 第一个字符串大于第二个字符串,则返回大于0的数字
  • 第一个字符串等于第二个字符串,则返回0
  • 第一个字符串小于第二个字符串,则返回小于0的数字
#include <stdio.h>
int main()
{
	char arr1[20] = "abcdef";
	char arr2[] = "abc";
	int ret = strncmp(arr1, arr2, 3);
	if (ret == 0)
	{
		printf("==\n");
	}
	else if (ret > 0)
	{
		printf(">\n");
	}
	else
	{
		printf("<\n");
	}
	return 0;
}

1.8 strstr(查找子串的函数)

const char * strstr ( const char * str1, const char * str2 ); char * strstr ( 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.
  • The matching process does not include the terminating null-characters, but it stops there.
#include<stdio.h>
int main()
{
	char arr1[] = "hello world";
	char arr2[] = "world";
	char* ret = strstr(arr1, arr2);
	if (ret == NULL)
	{
		printf("子串不存在\n");
	}
	else
	{
		printf("%s\n", ret);
     //若源字符串在目标字符串中存在,则会返回找到的地址
	}
	return 0;
}

模拟实现strstr函数:

#include <stdio.h>
#include <assert.h>
char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* s1 = str1;
	const char* p = str1;
	const char* s2 = str2;
	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[] = "abbbcedf";
	char arr2[] = "bbc";
	char* ret = my_strstr(arr1, arr2);
	if (ret == NULL)
	{
		printf("子串不存在\n");
	}
	else
	{
		printf("%s\n", ret);
	}
	return 0;
}

1.9 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 arr[] = "https://www.baidu.com";
	char cp[30] = { 0 };
	const char* sep = ":.";
	strcpy(cp, arr);//将数据拷贝一份,处理cp数组的内容

	char* ret = strtok(cp, sep);
	if (ret != NULL);
	printf("%s\n", ret);

	ret = strtok(NULL, sep);
	if (ret != NULL);
	printf("%s\n", ret);

	ret = strtok(NULL, sep);
	if (ret != NULL);
	printf("%s\n", ret);

	ret = strtok(NULL, sep);
	if (ret != NULL);
	printf("%s\n", ret);
	return 0;
}

//化简修改后:
#include <stdio.h>
#include <string.h>
int main()
{
	char arr[] = "https://www.baidu.com";
	char cp[30] = { 0 };
	const char* sep = ":.";
	strcpy(cp, arr);

	for (char* ret = strtok(cp, sep); ret != NULL; ret = strtok(NULL, sep))
	{
		printf("%s\n", ret);
	}
	return 0;
}

1.10 strerror

char * strerror ( int errnum );

返回错误码所对应的错误信息。
C语言的库函数在执行失败时,都会设置错误码

#include <stdio.h>
int main()
{
	printf("%s\n", strerror(0));
	printf("%s\n", strerror(1));
	printf("%s\n", strerror(2));
	printf("%s\n", strerror(3));
	printf("%s\n", strerror(4));
	printf("%s\n", strerror(5));
	printf("%s\n", strerror(6));
	return 0;
}

image.png

//errno - C语言设置的一个全局的错误码存放变量
#include <stdio.h>
#include <errno.h>
int main()
{
	FILE* pf = fopen("test.txt", "r");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}
	else
	{
		//
	}
	return 0;
}

字符分类函数:
image.png

#include <stdio.h>
#include <ctype.h>
int main()
{
	//isspace
	int a = isspace(' ');
	printf("%d\n", a);

	//isdigit
	int b = isdigit('x');
	printf("%d\n", b);

	//tolower
	printf("%c\n", tolower('w'));
}

image.png
字符转换:

int tolower ( int c );
int toupper ( int c );

1.11 memcpy

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

  • 函数memcpy从source的位置开始向后赋值num个字节的数据到destination的内存位置。
  • 这个函数在遇到'\0'的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的(memcpy负责拷贝两块独立空间中的数据)。
//memcpy
#include <stdio.h>
#include <string.h>
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7 };
	int arr2[10] = { 0 };
	memcpy(arr2, arr1, 28);

	float s1[] = { 1.0,2.0,3.0,4.0,5.0 };
	float s2[20] = { 0 };
	memcpy(s2, s1, 20);
	return 0;
}

模拟实现memcpy函数:

//模拟实现memcpy函数
#include <stdio.h>
#include <assert.h>
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()
{
	int arr1[] = { 1,2,3,4,5,6,7 };
	int arr2[10] = { 0 };
	my_memcpy(arr2, arr1, 28);
	int i = 0;
	for(i = 0; i < 5; i++)
	{
		printf("%d ", arr2[i]);
	}

	return 0;
}

1.12 memmove

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

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

模拟实现memmove函数:

//模拟实现memmove函数
#include <stdio.h>
#include <assert.h>
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 arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr1, arr1+2, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}

	return 0;
}

1.13 memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

  • 比较从ptr1和ptr2指针开始的num个字节
  • 返回值如下:

image.png
示例:

//memcmp
#include <stdio.h>
#include <string.h>
int main()
{
	int arr1[] = { 1,2,3,4,5 };
	int arr2[] = { 1,3,2 };
	int ret = memcmp(arr1, arr2, 12);
	printf("%d\n", ret);
	return 0;
}

1.14 memset

void * memset ( void * ptr, int value, size_t num );

  • 将ptr所指向的内存块的num个字节数设置为指定值

示例:

//memset
#include <stdio.h>
#include <string.h>
int main()
{
	char arr[] = "hello world";
	memset(arr, 'x', 5);
	memset(arr + 6, 'x', 3);
	printf("%s\n", arr);
	int arr[10] = { 0 };
	//把arr初始化为全1(这种方法是不可行的)
	memset(arr, 1, 40);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d\n", arr[i]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值