字符串函数---(1)

本文详细介绍了C语言中的四个重要字符函数:strlen(计算字符串长度)、strcpy(复制字符串)、strcat(连接字符串)和strcmp(字符串比较)。文中还提供了这些函数的模拟实现示例,帮助读者理解和掌握它们的用法。
摘要由CSDN通过智能技术生成

字符函数


前言

上一篇我们学习了字符函数,下来我们学习常见的字符串函数


1.strlen 的使用和模拟实现

在这里插入图片描述

size_t strlen(const char *str)

  • 字符串以 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包含 ‘\0’ )。
  • 参数指向的字符串必须要以 ‘\0’ 结束。
  • 注意函数的返回值为 size_t,是无符号的( 易错 )
  • strlen的使用需要包含头文件
  • 学会strlen函数的模拟实现
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strlen(const char *str)
{
	int cnt = 0;
	assert(str);
	while (*str)
	{
		cnt++;
		str++;
	}
	return cnt;
}
int main()
{
	char str[30] = "abcdefg";
	//printf("%d", strlen(str));
	int ret=my_strlen(str);
	printf("%d", ret);
	return 0;
}

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).
  • 源字符串必须以 ‘\0’ 结束。
  • 会将源字符串中的 ‘\0’ 拷贝到目标空间。
  • 目标空间必须足够大,以确保能存放源字符串。
  • 目标空间必须可修改。
  • 学会模拟实现。
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strcpy(char* dest, const char* src)
{
	char* ret = dest;
	assert(src && dest);
	while (*src)
	{
		*dest = *src;
		dest++;
		src++;
	}
	return ret;
}
int main()
{
	char str1[] = "hello world!";
	char str2[20] = {0};
	//strcpy(str2, str1);
	my_strcpy(str2, str1);
	printf("%s\n", str2);
	return 0;
}

在这里插入图片描述

3. 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.
  • 源字符串必须以 ‘\0’ 结束。
  • 目标字符串中也得有 \0 ,否则没办法知道追加从哪里开始。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改。
  • 字符串自己给自己追加,如何?

char* my_strcat(char* dest, char* src)
{
	char* ret = dest;
	while (*dest)
	{
		dest++;
	}
	while (*src)
	{
		*dest = *src;
		dest++;
		src++;
	}return ret;
}
#include<stdio.h>
#include<assert.h>
#include<string.h>
int main()
{	
	char str1[20] = { "hello," };
	char str2[20] = { "world!" };
	strcat(str1,str2);
	printf("%s\n", str1);
	return 0;
}

在这里插入图片描述

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.

  • 标准规定:
  • 第⼀个字符串大于第⼆个字符串,则返回大于0的数字
  • 第⼀个字符串等于第⼆个字符串,则返回0
  • 第⼀个字符串小于第⼆个字符串,则返回小于0的数字
  • 那么如何判断两个字符串? 比较两个字符串中对应位置上字符ASCII码值的大小。
#include<string.h>
#include<stdio.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{
	int ret = 0;
	assert(str1 != NULL);
	assert(str2 != NULL);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
			return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;

}
int main()
{
	char str1[] = "abcd";
	char str2[] = "abqd";
	//int ret = strcmp(str1, str2);
	int ret = my_strcmp(str1, str2);
	printf("%d", ret);
	return 0;
}

  • 45
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值