11.字符串+内存函数

目录

函数介绍

库函数的模拟实现


函数介绍

strlen:

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

#include <stdio.h>
#include <string.h>

int main()
{
	if (strlen("abc") - strlen("abcdef") > 0) {
		printf(">");
	}
	else {
		printf("<=");
	}
	return 0;
}

strcpy:

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

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

strcat:把原字符串的字符添加的目标字符串的后面

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

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

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[30] = "hello";
	char arr2[] = "world";
	strcat(arr1, arr2);
	printf("%s", arr1);
	return 0;
}

 

strcmp:比较对应位置上的字符大小

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

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


#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abedef";
	char arr2[] = "abc";
	int ret = strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}

 

strncpy:

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

拷贝num个字符从源字符串到目标空间。
如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
	char arr2[] = "hello";
	// arr2 的长度小于num,用'\0'来补充
	strncpy(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

 

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "xxxxxxxxxxxxxxxxxxxxxxxx";
	char arr2[] = "hlo";
	// arr2 的长度小于num,用'\0'来补充
	strncpy(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

 

strncat:将num个来自source中的字符添加到destination中,并且在后面补充'\0'

char * strncat ( char * destination, const char * source, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "hello";
	char arr2[] = "world";
	strncat(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "hello";
	char arr2[] = "world";
	strncat(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[30] = "hello********";
	char arr2[] = "worldaaaa";
	strncat(arr1, arr2, 5);
	printf(arr1);
	return 0;
}

 

strncmp:在str1和str中比较前num个字符串,若str1大于str2,返回大于0的数;若str1 小于str2,返回小于的数;若str1 等于str2,返回等于0的数

int strncmp ( const char * str1, const char * str2, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abcqqqqqqqqqqq";
	int ret = strncmp(arr1, arr2, 3);
	printf("%d\n", ret);
	return 0;
}

 

strstr:在str2中找str1,找到返回地址,否则NULL

char * strstr ( const char *str2, const char * str1);
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abcdefghigklmn";
	char arr2[] = "efg";
	char* ret = strstr(arr1, arr2);
	if (ret == NULL) {
		printf("找不到\n");
	} else {
		printf("%s\n", ret);
	}
	return 0;
}

 

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abcdefghigklmn";
	char arr2[] = "efgas";
	char* ret = strstr(arr1, arr2);
	if (ret == NULL) {
		printf("找不到\n");
	} else {
		printf("%s\n", ret);
	}
	return 0;
}

 

strtok :

strtok函数找第一个标记时,函数的第一个参数不是NULL

strtok函数找非第一个标记时,函数的第一个参数是NULL

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

char * strtok ( char * str, const char * sep );
#include <stdio.h>
#include <string.h>

int main()
{
	const char* p = "@.";
	char arr[] = "1234567@qq.com";
	char buf[50];
	strcpy(buf, arr);
	char* str = strtok(buf, 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()
{
	const char* p = "@.";
	char arr[] = "1234567@qq.com";
	char buf[50];
	strcpy(buf, arr);
	char* str = NULL;
	for (str = strtok(buf, p); str != NULL; str = strtok(NULL, p)) {
		printf("%s\n", str);
	}
	return 0;
}

 

strerror :返回错误码所对应的错误信息第地址

当库函数使用的时候,发生错误会把errno这个全局错误信息设置为本次执行库函数所产生的错误代码

0 --- "No Error"

1 --- 

2 --- 

char * strerror ( int errnum );
#include <stdio.h>
#include <string.h>

int main()
{
	for (int i = 0; i < 10; i++) {
		printf("%s\n", strerror(i));
	}
	return 0;
}

字符分类函数
函数如果他们的参数符合下列条件返回真
iscntrl任何控制字符
isspace空白字符:空格' ',换页符'\f',换行'\n', 回车'\r' ,制表符 '\t'或者垂直制表符'\v'
isdigit十进制数字 0 ~ 9
isxdigit十六进制数字,包括所有十进制数字,小写字母a ~ f,大写字母 A ~ F
islower小写字母 a ~ z
isupper大写字母 A ~ Z
isalpha字母a ~ z 或 A ~ Z
isalnum字母或数字,a ~ z, A ~ Z, 0 ~ 9
ispunct标点符号,任何不属于数字或者字母的图形字符(可打印)
isgreph任何图形字符
isprint任何可打印字符,包括图形字符和空白字符
tolower大写转换小写
toupper小写转换大写

memcpy:void*可以接受任意类型指针,

num字节

void * memcpy ( void * destination, const void * source, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	int arr3[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int arr4[5] = { 0 };
	memcpy(arr4, arr3, sizeof(int) * 5);
	for (int i = 0; i < 5; i++) {
		printf("%d ", arr4[i]);
	}
	return 0;
}

memmove:将数组中的一部分移动,有交叉内存的移动

void * memmove ( void * destination, const void * source, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	int arr[] = { 1, 2, 3, 4, 5 ,6 ,7 ,8, 9, 10 };
	memmove(arr + 2, arr, sizeof(int) * 5);
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

memcpy拷贝不重叠,memmove拷贝重叠 

memcmp:

比较ptr1和ptr2后面num个字节

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include <stdio.h>
#include <string.h>

int main()
{
	int arr1[] = { 1, 2, 3, 4, 5 };
	int arr2[] = { 1, 3, 3, 4, 5 };
	int ret = memcmp(arr1, arr2, 9);
	printf("%d\n", ret);
	return 0;
}

memset:把dest中count个字节填充为字符c

void *memset(void *dest, int c, size_t count);
#include <stdio.h>
#include <string.h>

int main()
{
	int arr[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	memset(arr, 0, sizeof(arr));
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

库函数的模拟实现

strlen

#include <stdio.h>
#include <assert.h>

int my_strlen(const char* str)
{
	assert(str);
	if (*str == '\0') {
		return 0;
	} else {
		return 1 + my_strlen(++str);
	}
}

int main()
{
	int len = my_strlen("abcdef");
	printf("%d\n", len);
	return 0;
}
#include <assert.h>

int my_strlen(const char* str)
{
	assert(str);
	int count = 0;
	while (*str) {
		count++;
		str++;
	}
	return count;
}

int main()
{
	int len = my_strlen("abcdef");
	printf("%d\n", len);
	return 0;
}
#include <stdio.h>
#include <assert.h>

int my_strlen(const char* str)
{
	assert(str);
	char* p = str;
	while (*p != '\0') {
		p++;
	}
	return p - str;
}

int main()
{
	int len = my_strlen("abcdef");
	printf("%d\n", len);
	return 0;
}

strcpy

#include <stdio.h>
#include <assert.h>

char* my_strcpy(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest++ = *src++) {
		;
	}
	return ret;
}

int main()
{
	char arr1[] = { 'a', 'b', 'c', 'd', 'e', 'f', '\0' };
	char arr2[20] = "aaaaaaaaaaaaaaaaaa";
	my_strcpy(arr2, arr1);
	printf(arr2);
	return 0;
}

strcat

char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest && src);
	while (*dest) dest++;
	while (*dest++ = *src++);
	return ret;
}

int main()
{
	char arr1[30] = "hello";
	char arr2[] = "world";
	my_strcat(arr1, arr2);
	printf("%s", arr1);
	return 0;
}

 

strstr

#include <stdio.h>
#include <assert.h>

char* my_strstr(const char* str, const char* substr)
{
	assert(str && substr);
	const char* cur = str;
	const char* s1 = str;
	const char* s2 = substr;
	if (*substr == '\0') return NULL;
	while (*cur) {
		s1 = cur;
		s2 = substr;
		while (*s1 && *s2 && *s1 == *s2) {
			s1++;
			s2++;
		}
		if (*s2 == '\0') {
			return (char*)cur;
		}
		cur++;
	}
	return NULL;
}

int main()
{
	char arr1[] = "abcdefghigklmn";
	char arr2[] = "efgas";
	char* ret = my_strstr(arr1, arr2);
	if (ret == NULL) {
		printf("找不到\n");
	} else {
		printf("%s\n", ret);
	}
	return 0;
}

 

strcmp

#include <stdio.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;
	}
	return -1;
}

int main()
{
	char arr1[] = "abedef";
	char arr2[] = "abc";
	int ret = my_strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}
#include <stdio.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++;
	}
	return *str1 - *str2;
}

int main()
{
	char arr1[] = "abedef";
	char arr2[] = "abc";
	int ret = my_strcmp(arr1, arr2);
	printf("%d\n", ret);
	return 0;
}

memcpy

#include <stdio.h>

void* my_memcpy(void* dest, void* src, size_t num)
{
	void* ret = dest;
	while (num--) {
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

int main()
{
	int arr3[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int arr4[5] = { 0 };
	my_memcpy(arr4, arr3, sizeof(int) * 5);
	for (int i = 0; i < 5; i++) {
		printf("%d ", arr4[i]);
	}
	return 0;
}

memmove

#include <stdio.h>
#include <assert.h>

void* my_memmove(void* dest, void* src, size_t num)
{
	void* ret = dest;
	assert(dest && src);
	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;
}
// dest  src 从前向后拷贝
// src  dest 从后向前拷贝
int main()
{
	int arr[] = { 1, 2, 3, 4, 5 ,6 ,7 ,8, 9, 10 };
	my_memmove(arr + 2, arr, sizeof(int) * 5);
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值