字符函数strlen/strcpy/strcat/strcmp

本文介绍了C语言中四个关键字符串处理函数:strlen用于计算字符串长度,strcpy用于字符拷贝,strcat用于字符串连接,以及strcmp用于字符串比较。每个函数的工作原理和使用注意事项都进行了详细说明。
摘要由CSDN通过智能技术生成

C语言本身是没有字符串类型的,但是C语言中对字符和字符串的处理却很频繁。

(一)字符函数

1.1 strlen - 求字符串的长度,以'\0'作为结束的标志,返回的是‘\0’前面出现的字符个数,返回值为size_t, 是无符号的;

size_t strlen ( const char * str );

头文件 <string.h>;

模拟实现strlen:

#include<stdio.h>
int my_strlen(char* ps)
{
	int count = 0;
	while(*ps)
	{
		count++;
		ps++;
	}
	return count;
}

int main()
{
	char arr[] = "abcdefg";
	int ret = my_strlen(arr);
	printf("%d", ret);

	return 0;
}

1.2 strcpy - 字符换拷贝;Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). 

char * strcpy ( char * destination, const char * source );//这里的返回值类型设置为char*是可以将返回值作为参数实现链式访问;

模拟实现strcpy

#include<stdio.h>
#include<assert.h>
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest && src);
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr1[20] = "**************";
	char arr2[] = "abcdefg";
	my_strcpy(arr1, arr2);//将arr2拷贝到arr1里面
	printf("%s", arr1);

	return 0;

总结:源字符串arr2必须包含'\0';并且'\0'会一起拷贝到arr1中;arr1的空间必须足够大且可变;

1.3 strcat - 字符串连接;

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

Concatenate strings

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 and source shall not overlap.

模拟实现strcat :

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

int main()
{
	char arr1[30] = "today is my ";
	char arr2[] = "lucky day";
	printf("%s", my_strcat(arr1, arr2));
	return 0;
}

总结:源字符串必须以‘\0’结束,并且目标空间必须足够大,可修改;

注意:字符串自身不能给自身追加,就是比如 my_strcat(arr1, arr1),这样'\0'会被覆盖,就会找不到停下来的标志;

1.4 strcmp - 字符串比较,

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

Compares the C string str1 to the C string str2.

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.

Returns an integral value indicating the relationship between the strings:

Return Value

return valueindicates
<0the first character that does not match has a lower value in ptr1 than in ptr2
0the contents of both strings are equal
>0the first character that does not match has a greater value in ptr1 than in ptr2

模拟实现strcmp :

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);//字符在内存中是以ASCII码值存储的;

}

int main()
{
	char arr1[] = "abcdefg";
	char arr2[] = "abcde";
	int ret = my_strcmp(arr1, arr2);

	if (ret == 0)
		printf("arr1和arr2相等\n");
	else
		printf("arr1和arr2不相等\n");
	return 0;
}

总结:以上的字符串长度不受限制,只关注\0;

可以和strncpy,strncat,strncmp进行比较学习;

strlenstrcpystrcmpstrcat都是C语言中的字符串处理函数。其中strlen是用来计算字符串的长度的函数,其语法为:size_t strlen(const char *str),其中str表示需要计算长度的字符串。该函数的返回值为一个size_t类型的无符号整数,表示字符串的长度,不包括空字符('\0')。例如,若str为"hello",则strlen(str)的结果为5。 strcpy是用于将一个字符串复制到另一个字符串中的函数。其语法为:char *strcpy(char *dest, const char *src),其中dest表示目标字符串的地址,src表示需要复制的源字符串的地址。该函数会将src中的内容复制到dest中,并返回dest的值。例如,若src为"hello",dest为一个长度为10的字符数组,则执行strcpy(dest, src)后,dest数组中就存储了"hello"这个字符串。 strcmp是用于比较两个字符串是否相等的函数。其语法为:int strcmp(const char *s1, const char *s2),其中s1和s2表示需要比较的两个字符串。该函数会先比较s1和s2的第一个字符,若相同则继续比较后面的字符,直到不相等为止。返回值为一个整数,若s1>s2则返回正整数,若s1<s2则返回负整数,若s1=s2则返回0。例如,若s1为"abc",s2为"abd",则执行strcmp(s1, s2)后,返回的值为负整数。 strcat是用于将两个字符串连接起来的函数。其语法为:char *strcat(char *dest, const char *src),其中dest表示目标字符串的地址,src表示需要添加的源字符串的地址。该函数会将src中的内容添加到dest的末尾,并返回dest的值。例如,若dest为"hello",src为"world",则执行strcat(dest, src)后,dest数组中存储了"helloworld"这个字符串。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值