超详细C语言的字符串函数讲解

字符串函数

前言

C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者字符数组中。字符串常量 适用于那些对它不做修改的字符串函数
接下来本文就是对于介绍一些常用的字符串函数进行讲解
所有字符串相关的函数都放在string.h的头文件中

1. strlen😋

size_t strlen ( const char * str ) ;

字符串是以’\0’结尾的,那么strlen就是返回\0之前的字符个数
size_t是定义的一个宏,本质上是一个 unsigned int 类型
那么我们就有思路来实现strlen了只要遍历字符串找’\0’
每找一个字符count就++
找到\0了那么就返回count
下面我们就来模仿一下如何实现

size_t myStrlen(const char* str){
	if(str == NULL){
		return 0; // 合法性校验
	}
	size_t count = 0;
	while(*str != '\0'){
		count++;
		str++;
	}
	return count;
}

2.strcpy🤨

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

把源字符串source的字符串拷贝到目的字符串dest中
注意会把source的’\0’也拷贝到dest中
为了避免溢出,dest要有足够的空间来接受从source拷贝过来的字符串
代码实现:

char* myStrcpy(char* dest,const char* source){
	assert(source != NULL);
	assert(dest != NULL);// 首先都要进行参数校验合法性
	int i = 0;
	while(source[i] != '\0'){
		dest[i] = source[i];
		i++;
	}
	dest[i] = '\0';
	return dest;
}

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.
追加一个从源字符串拷贝过来的字符串到目的字符串中,目的字符串中的’\0’
会被源字符串中的第一个字符给覆盖掉,并且’\0’会包含在新生成的字符串中
比如:
char str1[] = “hello”;
char str2[] = “world”;
使用strcat后, str1就会变为"helloworld"
原来str1的’\0’会被’w’字符给覆盖掉
代码实现:

char * myStrcat ( char * dest, const char * source ){
	assert(source != NULL);
	assert(dest != NULL);
	int destination = 0;
	while(dest[destination] != '\0'){
		destination++; // 这一步相当于找到dest的末尾位置
	}
	int i = 0
	while(source[i] != '\0'){
		dest[i + destination] = source[i];
		i++;
	}
	dest[i + destination] = '\0';
	return dest;
}

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 characters differ or until a terminating null-character is reached.
代码实现:

int myStrcmp(const char* str1,const char* str2){
	assert(str1 != NULL);
	assert(str2 != NULL);
	const char* p1 = str1;
	const char* p2 = str2;
	while(*p1 != '\0' && *p2 != '\0'){
	// 规定返回1则代表str1比str2大
		if(*p1 > *p2){
			return 1;
		}else if(*p1 < *p2){
			return -1;
		}else{
			p1++;
			p2++;
			// 相等则都加1去比较下一个字符
		}
	}
	// 假设此时str1为"abcd" str2为"ab"
	// 此时这个情况下str1没有到末尾而str2到了末尾
	// 需要比较这个情况下的大小
	// 谁小就是谁到了\0
	if(*p1 > *p2){
		return 1;
	}else if(*p1 < *p2){
		return -1;
	}else{
		return 0;
		// 如果p1和p2同时触发\0则认为相等
	}
}

5.strstr😫

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

Locate substring
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.

功能是判断str1中是否包含str2这个子串
如果包含则返回在str1中的指向第一个出现str2首字符的指针
注意事项:这个比较过程不会包括’\0’但它会在’\0’的地方停止比较
这个函数比较复杂举个例子:
char str1[] = “hello world”;
char str2[] = “llo”;
判断str1中是否包含str2
显然是包含的,那么它就会返回一个指向’l’的指针
这里我们引入三个指针,一个black指向str1的首元素,一个point指针指向str2的首元素,一个red指针用来和point比较
示意
比较的步骤:
在这里插入图片描述

代码实现:

const char* myStrstr(const char* str1,const char* str2){
	assert(str1 != NULL);
	assert(str2 != NULL);
	assert(*str1 != '\0');
	assert(*str2 != '\0');
	const char* black = str1;
	while(*black != '\0'){
	char* point = str2;
	char* red = black;
		if(*point != '\0' && *red != '\0' && *point == *red){
			red++;
			point++;
			// 相等的话就都加1比较下一个字符
		}
		// 上述循环有三种结束情况
		// 1.*point == '\0' 那么就找到了子串直接返回black
		// 2.*red == '\0' 可以直接返回0,将相当与red已经到了str1的最后 
		// 3. *point != *red 不相等就不用比较直接进入下次循环
		if (*point == '\0') {
			return black;
		}
		black++;
	}
	return NULL;
	// 若是循环结束了就是没有找到直接返回NULL
}

这节的内容就到这啦友友们,有什么疑问欢迎私信我哦
还有两个操作内存的函数是以字节为单位
memmove memcpy
这个就下次博客再讲吧😏😏😏

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gopher333

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值