字符函数和字符串函数

1.字符函数:

1.1.字符分类函数:

c语言中有一类函数专门做字符分类(判断一个字符属于什么类型)

需包含头文件ctype.h

在这里插入图片描述

1.2.字符转换函数:

c语言中提供了两个字符转换函数:

1.tolower:大写转小写
2.toupper:小写转大写

注:这两个函数返回值为一个字符,传入的参数亦为一个字符。

2.字符串函数:

2.1.strcpy(字符串拷贝):

函数功能:Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point)
/将source指向的C字符串复制到destination指向的数组中,包括结束的null字符’\0’(并在该点停止)

注意事项:
1.源字符串必须以’\0’结束
2.会将’\0’拷贝到目标字符串中,所以打印时只会显示’\0’以前的内容
3.目标空间必须足够大,以确保能存放源字符串。
4.若强行将长字符串拷贝到短字符串中,会强行拷贝,覆盖后面的空间,但运行时会发出警告

模拟实现:
版本1:

void my_strcpy(char* des,char*sor)
{
	while(*des++=*sor++)//'\0'的值为0,0被赋值时循环结束
	{
		;
	}
}

版本2(返回目标字符串数组起始位置便于观察):

char* my_strcpy(char* des,char*sor)
{
	char*ret=des;
	while(*des++=*sor++)//'\0'的值为0,0被赋值时循环结束
	{
		;
	}
	return ret;
}

最后直接从ret指向对象开始打印

2.2.strcat(字符串追加):

函数功能: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.
/将源字符串的内容追加到目标字符串。destination中的结束null字符被source的第一个字符覆盖,并且在destination中由两者串联形成的新字符串的末尾包含一个空字符。将源字符串的副本追加到目标字符串。destination中的结束null字符被source的第一个字符覆盖,并且在destination中由两者串联形成的新字符串的末尾包含一个空字符。

注意事项:
1.源字符串必须以’\0’结束,它决定了追加多少
2.目标字符串中也要有’\0’,它决定了从哪里开始追加
3.目标空间要足够大

模拟实现:

char* my_strcat(char* des,const char* sor)//不希望sor指向的源头内容被修改
{
	char* ret=des;
	assert(des&&sor);
	while(*des!='\0')
	{
		des++;
	}
	while(*des++=*sor++)//'\0'的值为0,0被赋值时循环结束
	{
		;
	}
	return ret;
}

2.3.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.
/这个函数开始比较每个字符串的第一个字符。如果它们彼此相等,则继续执行以下对,直到字符不同或达到终止空字符为止。

标准规定:
1◦ 第⼀个字符串大于第二个字符串,则返回⼤于0的数字
2◦ 第⼀个字符串等于第二个字符串,则返回0
3◦ 第⼀个字符串小于于第二个字符串,则返回小于0的数字
4◦ 那么如何判断两个字符串? 比较两个字符串中对应位置上字符ASCII码值的大小。

模拟实现:

int my_strcmp (const char * str1, const char * str2)
{
 	int ret = 0 ;
 	assert(src != NULL);
 	assert(dest != NULL);
	 while(*str1 == *str2)
 	{
 		if(*str1 == '\0')
 		return 0;
 		str1++;
 		str2++;
 	}
 	return *str1-*str2;
}

缺陷:
如果在字符串中间出现’\0’可能会出现误判

2.4.strstr(字符串查找):

函数功能: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第一次出现的指针,如果str2不是str1的一部分,则返回空指针。匹配过程不包括结束的空字符,但它到此为止。

注意事项:
1.每次查找时应该使用一个指针记录在目标字符串内开始查找的起始位置
2.因涉及多次查找,所以应该对传入的指针进行备份,保证能够回溯各起始位置进行下一轮查找
3.目标字符串更长

模拟实现:

char* my_strstr(const char*str1,const char* str2)
{
	char*des=NULL;
	char*sor=NULL;
	char*cp=str1;
	if(*str2=='\0')
	{
		return ((char*)str1);
	}
	while(*cp!='\0')
	{
		des=cp;
		sor=(char*)str2;
		while(*des==*sor&&*des&&*sor)
		{
			des++;
			sor++;
		}
		if(*sor=='\0')
		return cp;
	}
	return NULL;
}

2.5.strtok(字符串切割函数)

函数声明:

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

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

使用方法:

int main()
{
 	char arr[] = "192.168.6.111";
 	char* sep = ".";
 	char* str = NULL;
 	for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
 	{
 		printf("%s\n", str);
 	}
 	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

碳酸不酸鸭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值