当前位置:网站首页>求字符串函数和长度不受限制的字符串函数的详解
求字符串函数和长度不受限制的字符串函数的详解
2022-07-03 15:16:00 【小蜗牛向冲】
前言
作者:小蜗牛向前冲
名言:我可以接收失败,但我不能接收放弃
如果觉的博主的文章还不错的话,还请
点赞,收藏,关注支持博主。如果发现有问题的地方欢迎*大家在评论区指正。
这期博客主要为大家介绍一些字符串函数实现及注意点。
求字符串长度
strlen函数
定义
传参
传字符串过去或者字符串的首地址。
该函数是用来求字符串长度,遇到'\0'就停止计算,返回类型是size_t(unsigned int ),
头文件是<string.h>,下面我们继续介绍是如何使用。
strlen使用举例:
#include<stdio.h>
#include<string.h>
int main()
{
char arr []= "abcdef";
int ret = strlen(arr);
printf("ret = %d\n", ret);
return 0;
}
这里直接传字符串也是可以的。
我们知道了strlen函数的使用,为了让我们更好的理解strlen函数。下面我将用三种方式去模拟实现strlen函数。
1 计数器的方式
//计数器实现
size_t my_strlen(const char* str)
{
assert(str);//断言字符串不为空字符串
int count = 0;//记录字符的个数
while (*str != '\0')
{
count++;
str++;
}
return count;
}
2 递归实现
size_t my_strlen_1(const char* str)
{
assert(str);//断言字符串不为空字符串
if (*str != '\0')
{
return 1 + my_strlen_1(str + 1);
}
else
{
return 0;
}
}
3 指针-指针实现
//指针-指针的方式实现
int my_strlen(const char* str)
{
assert(str);//断言字符串不为空字符串
char* start = str;//将arr的地址给start
char* end = str;//arr地址给end
while (*end != '\0')
{
//如果*end不等于'\0'
end++;//指针++
}
return end - start;//指针-指针就是等于长度
}
我们知道strlen是如何计算字符串的,那该函数的使用有什么要注意的?
注意点:
字符串是以'\0'作为结束的标志,strlen是统计字符串'\0'之前的字符。
参数指向的字符串必须要以 '\0' 结束。
字符串的返回类型是size_t类型。
长度不受限制的字符串函数
strcpy字符串拷贝函数
strcat字符串追加函数
strcmp字符串比较函数
strcpy字符串拷贝函数
定义
参数
strcpy( 目标字符串,源字符串);
strcpy是一个字符串拷贝函数,能将源字符串拷贝到目标字符串中,
返回类型是char*(返回的是目标字符串首的地址),
头文件是<string.h>。
strcpy使用举例:
int main()
{
char arr1[20] = "abcdefg" ;
char arr2[] = "ping";
strcpy(arr1, arr2);//拷贝字符串
printf("%s\n", arr1);
return 0;
}
这里我们要注意的是strcpy不仅仅把arr2中的字符拷贝过去,还把'\0'拷贝过去了。
模拟实现strcpy
char* my_strcpy(char* dest, const char* source)
{
assert(dest && source);//断言
char* ret = dest;//保存好目标字符串的首地址
while (*dest++ = *source++)
{
;
}
return ret;
}
strcpy函数注意点:
源字符串必须以 '\0' 结束。
会将源字符串中的 '\0' 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。
目标空间必须可变。
strcat字符串追加函数
定义
参数
strcat( 目标字符串,源字符串);
strcat是字符串追加函数,能够将源字符串的字符追加到目标字符串中。
返回类型是char*(返回的是目标字符串首的地址),
头文件是<string.h>。
strcat使用举例:
int main()
{
char arr1[20] = "allow";
char arr2[] = "act";
strcat(arr1, arr2);//追加字符串
printf("%s\n", arr1);
return 0;
}
strcat模拟实现
char* my_strcat(char* dest, const char* scoure)
{
assert(dest && scoure);//断言
char* ret = dest;
//找到目标字符串的'\0'
while (*dest!=NULL)//这里不要用(*dest++)这样会导致跳过了'\0'
{
dest++;
}
//进行字符串的追加
while (*dest++ = *scoure++)
{
;
}
return ret;
}
注意点:strcat函数
源字符串必须以 '\0' 结束。
目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。
不能自己给自己追加字符串,程序会崩溃。
strcmp字符串比较函数
定义
参数
strcmp(string1,string2);
返回值
strcmp是字符串比较函数,该函数是从二个字符串的元素开始,进行比较(比较本质为字母ascii码值的大小)。
头文件是<string.h>。
strcmp使用举例:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
\
int main()
{
char* p1 = "abcdef";
char* p2 = "afe";
int ret = strcmp(p1, p2);
if (ret > 0)
{
printf("常量字符串p1大于常量字符串p2\n");
}
else if (ret < 0)
{
printf("常量字符串p1小于常量字符串p2\n");
}
else
{
printf("常量字符串p1等于常量字符串p2\n");
}
return 0;
}
这里不少人可能会有疑问了,怎么是字符串p1<字符串p2,明明字符串p1有6个字符而字符串p2只有3个字符。
这里我们就要分清楚strcmp函数不是比较字符串中的字符数量,以p1和p2举例,首先比较p1中的a和p2中的a发现二者相等,就继续往下比较,b和f的大小,很明显f是比b大的,所以strcmp就返回一个小于0的数。
我们继续模拟首先strcmp函数加深理解
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);//断言
while (*str1== *str2)
{
//判断str1和str2是否相等
if (*str1 == '\0')//当str1==str2
{
return 0;
}
str1++;
str2++;
}
//str1与str2不相等,比较大小
return (*str1 - *str2);
}
strcmp函数
注意点:
理解好strcmp比较方法,便于我们正确的使用strcmp函数。
喜欢的话点个赞在走吧!
边栏推荐
- Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
- The state does not change after the assignment of El switch
- 使用JMeter对WebService进行压力测试
- 【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
- Composite type (custom type)
- Redis cache penetration, cache breakdown, cache avalanche solution
- App全局异常捕获
- Troubleshooting method of CPU surge
- 基础SQL教程
- Use of Tex editor
猜你喜欢
【pytorch学习笔记】Datasets and Dataloaders
Halcon与Winform学习第一节
[pytorch learning notes] datasets and dataloaders
Functional modules and application scenarios covered by the productization of user portraits
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
Yolov5 series (I) -- network visualization tool netron
北京共有产权房出租新规实施的租赁案例
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
Unity hierarchical bounding box AABB tree
第04章_逻辑架构
随机推荐
Using Tengine to solve the session problem of load balancing
Using notepad++ to build an arbitrary language development environment
【Transform】【NLP】首次提出Transformer,Google Brain团队2017年论文《Attention is all you need》
[cloud native training camp] module VIII kubernetes life cycle management and service discovery
视觉上位系统设计开发(halcon-winform)
Reentrantlock usage and source code analysis
Global and Chinese markets for sterile packaging 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of postal automation systems 2022-2028: Research Report on technology, participants, trends, market size and share
Redis cache penetration, cache breakdown, cache avalanche solution
视觉上位系统设计开发(halcon-winform)-5.相机
Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)
Global and Chinese market of transfer case 2022-2028: Research Report on technology, participants, trends, market size and share
Yolov5 advanced nine target tracking example 1
Final review points of human-computer interaction
5.4-5.5
【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
Global and Chinese markets of AC electromechanical relays 2022-2028: Research Report on technology, participants, trends, market size and share
2022/02/14
视觉上位系统设计开发(halcon-winform)-3.图像控件
Using multipleoutputs to output multiple files in MapReduce