字符串函数的使用和模拟实现(strcmp,strlen,strstr,strcpy,strcat)

1.strcmp

使用说明

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

比较两个字符串
将 C 字符串 str1 与 C 字符串 str2 进行比较。
比较的是ASCII编码
此函数开始比较每个字符串的第一个字符。如果它们彼此相等,则继续以下对,直到字符不同或达到终止空字符。
若str1> str2,则返回大于0的数;
若str1>==str2,则返回等于0的数;
若str1< str2,则返回小于0的数;
例如:比较字符串"abc"与"abq",代码如下:

#include<string.h>
#include<stdio.h>
int main()
{
	char source[50] = "abc";
	char str[20] = "abq";
	int ret = strcmp(source, str);
	printf("%d ", ret);
	return 0;
}

模拟实现

#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* a, const char* b)
{
	assert(a);
	assert(b);
	//断言以防空指针
	while (*a == *b && *a && *b)
	{
		a++;
		b++;
	}
	//while循环直到*a与*b不相同或为'\0'
	return *a - *b;
	//返回的差值就是结果
}

int main()
{
	char source[50] = "abc";
	char str[20] = "abq";
	int ret = my_strcmp(source, str);
	printf("%d ", ret);

	return 0;
}

2.strlen

#include<string.h>
size_t strlen ( const char * str );

使用说明

获取字符串长度
返回 C 字符串 str 的长度。
C 字符串的长度由终止空字符确定:C 字符串的长度与字符串开头和终止空字符之间的字符数一样长(不包括终止空字符本身)
例如:求字符串“abcefghijklmn”的长度,代码如下:

#include<string.h>
#include<stdio.h>
int main()
{
	char str[20] = "abcefghijklmn";
	int ret = strlen( str);
	printf("%d ", ret);

	return 0;
}

模拟实现

#include<stdio.h>
#include<assert.h>
size_t my_strlen(const char* a)
{
	assert(a);
	size_t count = 0;
	while (*a++)
	{
		count++;
	}
	//while循环直到*a为'\0',并在循环中记录count的值
	return count;
}

int main()
{
	char str[20] = "abcefghijklmn";
	size_t ret = my_strlen(str);
	printf("%zd ", ret);

	return 0;
}

3.strstr

#include<string.h>
char *strstr(const char *haystack, const char *needle)

使用说明

查找子字符串
返回指向 str1 中第一次出现的 str2 的指针,如果 str2 不是 str1 的一部分,则返回一个空指针。
匹配过程不包括终止空字符,但它到此为止。
例如:在"abbbbbccdef"字符串str1中查找"bbc"子字符串str2,代码如下:

#include<string.h>
#include<stdio.h>
int main()
{
	char* ret = strstr("abbbbbccdef","bbc");
	printf("%s ", ret);
	return 0;
}

模拟实现

#include<stdio.h>
#include<assert.h>
char* my_strstr(const char* a, const char* b)
{
	assert(a);
	assert(b);
	if (!*b) return a;
	//如果*b为空字符串则直接返回a
	char* n = a;
	while (*n)
	{
		char* m = n;
		char* tmp = b;
		while (*tmp==*m&&*tmp&&*m)
		{
			tmp++;
			m++;
		}
		//while循环,
		//当*tmp='\0'时,说明a中存在包含b的子字符
		if (!*tmp)
			return n;
		else
		{
			n++;
		}
	}
	return NULL;
	//若程序未在前面返回就只能说明a中没有b的子字符
}

int main()
{
	char* ret = my_strstr("abbbbbccdef","bbc");
	printf("%s ", ret);
	return 0;
}

4.strcpy

#include<string.h>
char * strcpy ( char * destination, const char * source );

使用说明

将源指向的 C 字符串复制到目标指向的数组中,包括终止的 null 字符(并在该点停止)。
为避免溢出,目标指向的数组的大小应足够长,以包含与源相同的 C 字符串(包括终止空字符),并且不应在内存中与源重叠。
例如:将大小为13的字符"No possible"复制到初始空间为50的字符串中。例如:

#include<string.h>
#include<stdio.h>
int main()
{
	char destination[50] = "i love you";
	char source[20] = "No possible";
	char* ret = strcpy(destination, source);
	printf("%s ", ret);
	return 0;
}

模拟实现

#include<assert.h>
#include<stdio.h>
char* my_strcpy(char* a, const char* b)
{
	assert(a);
	assert(b);
	char* ret = a;
	while (*a++ = *b++)
	{
		;
	}		
	//利用while循环复制*b
	return ret;
}

int main()
{
	char destination[100] = "i love you";
	char source[20] = "No possible...";
	char* ret = my_strcpy(destination, source);
	printf("%s ", ret);
	return 0;
}

5.strcat

#include<string.h>
char * strcat ( char * destination, const char * source )

使用说明

连接字符串
将源字符串的副本追加到目标字符串。目标中的终止空字符被源的第一个字符覆盖,并且在目标中由两者串联形成的新字符串的末尾包含一个空字符。
目的地和来源不得重叠。
例如:我要连接字符串str1与str2,并且str1所占空间要大于str1与str2相加的有效字符+1,例如:

#include<stdio.h>
#include<string.h>
int main()
{
	char destination[100] = "i love you ";
	char source[20] = "No possible...";
	char* ret = strcat(destination, source);
	printf("%s ", ret);
	return 0;
}

模拟实现

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* a, const char* b)
{
	assert(a);
	assert(b);
	char* ret = a;
	while (*a) a++;
	//找到a中的第一个'\0'
	while (*a++ = *b++) {}
	//利用while循环在a中第一个'\0'之后包括'\0'复制*b
	return ret;
}

int main()
{
	char destination[100] = "i love you ";
	char source[20] = "No possible...";
	char* ret = strcat(destination, source);
	printf("%s ", ret);
	return 0;
}
  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蒋志昂

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

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

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

打赏作者

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

抵扣说明:

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

余额充值