当前位置:网站首页>求字符串函数和长度不受限制的字符串函数的详解
求字符串函数和长度不受限制的字符串函数的详解
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函数。
喜欢的话点个赞在走吧!

边栏推荐
- 4-29——4.32
- redis缓存穿透,缓存击穿,缓存雪崩解决方案
- TPS61170QDRVRQ1
- Relationship between truncated random distribution and original distribution
- [pytorch learning notes] transforms
- Introduction, use and principle of synchronized
- "Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
- 使用JMeter对WebService进行压力测试
- Global and Chinese market of iron free motors 2022-2028: Research Report on technology, participants, trends, market size and share
- Yolov5 advanced seven target tracking latest environment construction (II)
猜你喜欢

Redis主从、哨兵、集群模式介绍
![MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)](/img/cd/2e4f5884d034ff704809f476bda288.png)
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)

Centos7 deployment sentry redis (with architecture diagram, clear and easy to understand)

Reentrantlock usage and source code analysis

Composite type (custom type)

视觉上位系统设计开发(halcon-winform)

Pytoch deep learning and target detection practice notes

4-29——4.32

北京共有产权房出租新规实施的租赁案例

解决pushgateway数据多次推送会覆盖的问题
随机推荐
TPS61170QDRVRQ1
[Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
Relationship between truncated random distribution and original distribution
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
Nppexec get process return code
在MapReduce中利用MultipleOutputs输出多个文件
[transform] [NLP] first proposed transformer. The 2017 paper "attention is all you need" by Google brain team
[attention mechanism] [first vit] Detr, end to end object detection with transformers the main components of the network are CNN and transformer
Detailed comments on MapReduce instance code on the official website
2022/02/14
Neon global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Redis single thread problem forced sorting layman literacy
Zero copy underlying analysis
Tencent internship interview sorting
Functional modules and application scenarios covered by the productization of user portraits
Vs+qt application development, set software icon icon
How does vs+qt set the software version copyright, obtain the software version and display the version number?
Chapter 04_ Logical architecture
XWiki Installation Tips
Yolov5 series (I) -- network visualization tool netron
