免费企业网站建设哪家/如何销售自己产品方法有哪些
各位CSDN的uu们你们好呀,今天小雅兰来给大家介绍一个全新的知识点,就是字符函数和字符串函数啦,其实其中有些函数我之前已经学习过了,比如strlen、strcpy;也有一些之前不是很熟悉的函数,比如strstr、strtok、strerror等等。话不多说啦,现在,让我们进入字符函数和字符串函数的世界吧
求字符串长度
strlen
长度不受限制的字符串函数
strcpy
strcat
strcmp
长度受限制的字符串函数介绍
strncpy
strncat
strncmp
C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在
常量字符串中或者字符数组中。
字符串常量适用于那些对它不做修改的字符串函数.
strlen
![]()
size_t strlen ( const char * str );
字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() {char arr[] = "abc\0def";int len = strlen(arr);printf("%d\n", len);return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() {char arr[] = "abcdef";int len = strlen(arr);printf("%d\n", len);return 0; }
abcdef后面隐藏了一个\0
参数指向的字符串必须要以 '\0' 结束。
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() {char arr[3] = { 'a','b','c'};int len = strlen(arr);printf("%d\n", len);return 0; }
如果字符串不以\0结束,那么,结果就是一个随机值
注意函数的返回值为size_t,是无符号的
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> int main() {const char* str1 = "abcdef";const char* str2 = "bbb";if (strlen(str2) - strlen(str1) > 0){printf("str2>str1\n");}else{printf("srt1>str2\n");}return 0; }
模拟实现strlen
三种方式:
1.计数器的方式
2.递归的方式
3.指针-指针的方式
函数递归+青蛙跳台阶——“C”_认真学习的小雅兰.的博客-CSDN博客
指针——“C”_认真学习的小雅兰.的博客-CSDN博客
1.计数器的方式
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<assert.h> int my_strlen(const char* str) {//计数器的方式int count = 0;assert(str != NULL);while (*str != '\0'){str++;count++;}return count; } int main() {char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0; }
2.递归的方式
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> //不能创建临时变量,求字符串的长度 int my_strlen(const char * str) {if(*str=='\0')return 0;elsereturn 1 + my_strlen(str+1); }int main() {char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0; }
3.指针-指针的方式
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> //指针-指针的方式 int my_strlen(char * s) {char * p = s;while( * p != '\0')p++;return p-s; }int main() {char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0; }
strcpy
![]()
char* strcpy(char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
源字符串必须以 '\0' 结束。
#include<stdio.h> #include<string.h> int main() {char arr1[3] = { 'a','b','c' };char arr2[20] = "xxxxxx";strcpy(arr2, arr1);printf("%s\n", arr2);return 0; }
这样程序直接崩溃了
会将源字符串中的 '\0' 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。
#include<stdio.h> #include<string.h> int main() {//错误的示范char arr1[20] = "abcdefghi";char arr2[3] = "";strcpy(arr2, arr1);printf("%s\n", arr2);return 0; }
目标空间必须可变。
#include<stdio.h> #include<string.h> int main() {//错误的示范char* p = "abcdefghi";char arr2[20] = "hehe";strcpy(p, arr2);printf("%s\n", p);return 0; }
模拟实现strcpy
#include<stdio.h> #include<string.h> //1.参数顺序 //2.函数的功能,停止条件 //3.assert //4.const修饰指针 //5.函数返回值 //6.题目出自《高质量C/C++编程》书籍最后的试题部分 //返回的是目标空间的起始地址 #include<assert.h> char * my_strcpy(char * dest, const char* src) {char * ret = dest;assert(dest!=NULL);assert(src != NULL);while ((*dest++ = *src++)){;}return ret; } int main() {char arr1[] = "hehe";char arr2[20] = { 0 };my_strcpy(arr2, arr1);printf("%s\n", arr2);return 0; }
strcat
char * strcat ( char * destination, const char * source );
#include<stdio.h> #include<string.h> int main() {char arr1[20] = "hello ";char arr2[] = "world";//追加strcat(arr1, arr2);printf("%s\n", arr1);return 0; }
![]()
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' 结束。
#include<stdio.h> #include<string.h> int main() {char arr1[20] = "hello \0xxxxxxxxxxxx";char arr2[] = "world";//追加strcat(arr1, arr2);printf("%s\n", arr1);return 0; }
目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。
模拟实现strcat
#include<stdio.h> #include<assert.h> char* my_strcat(char* dest, const char* src) {char* ret = dest;assert(dest != NULL);assert(src != NULL);//找目标空间的\0while (*dest!='\0'){dest++;}//拷贝while ((*dest++ = *src++)){;}return ret; } int main() {char arr1[20] = "hello ";char arr2[] = "world";//追加my_strcat(arr1, arr2);printf("%s\n", arr1);return 0; }
绝对不能自己给自己追加!!!
#include<stdio.h> #include<assert.h> char* my_strcat(char* dest, const char* src) {char* ret = dest;assert(dest != NULL);assert(src != NULL);//找目标空间的\0while (*dest != '\0'){dest++;}//拷贝while ((*dest++ = *src++)){;}return ret; } int main() {char arr1[20] = "bit";//追加my_strcat(arr1, arr1);printf("%s\n", arr1);return 0; }
strcmp
![]()
“abcdef"=="bbcdef",这里比较的是两个字符串首字符的地址,而并不是字符串的内容
比较两个字符串内容的时候,不能使用==,应该使用strcmp
int strcmp ( const char * str1, const char * 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.
标准规定:
- 第一个字符串大于第二个字符串,则返回大于0的数字
- 第一个字符串等于第二个字符串,则返回0
- 第一个字符串小于第二个字符串,则返回小于0的数字
#include<stdio.h> #include<string.h> #include<assert.h> int main() {char arr1[] = "abcdef";char arr2[] = "bbcdef";int ret = strcmp(arr1, arr2);printf("%d\n", ret);return 0; }
模拟实现strcmp
#include<stdio.h> #include<string.h> #include<assert.h> int my_strcmp(const char* str1, const char* str2) {assert(str1 != NULL);assert(str2 != NULL);while (*str1 == *str2){if (*str1 == '\0'){return 0;}str1++;str2++;}if (*str1 > *str2){return 1;}else{return -1;} }int main() {char arr1[] = "abcdef";char arr2[] = "bbcdef";int ret = my_strcmp(arr1, arr2);printf("%d\n", ret);return 0; }
另一种写法:
#include<stdio.h> #include<string.h> #include<assert.h> int my_strcmp(const char* str1, const char* str2) {assert(str1 != NULL);assert(str2 != NULL);while (*str1 == *str2){if (*str1 == '\0'){return 0;}str1++;str2++;}return *str1 - *str2; } int main() {char arr1[] = "abcdef";char arr2[] = "bbcdef";int ret = my_strcmp(arr1, arr2);printf("%d\n", ret);return 0; }
strncpy
![]()
char * strncpy ( char * destination, const char * source, size_t num );
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
拷贝num个字符从源字符串到目标空间。
如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
#include<stdio.h> #include<string.h> #include<assert.h> int main() {char arr1[] = "abcdef";char arr2[5] = { 0 };strncpy(arr2, arr1, 3);printf("%s\n", arr2);return 0; }
strncat
![]()
char * strncat ( char * destination, const char * source, size_t num );
- Appends the first num characters of source to destination, plus a terminating null-character.
- If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
#include<stdio.h> #include<string.h> #include<assert.h> int main() {char arr1[20] = "hello \0xxxxxxxx";char arr2[] = "world";strncat(arr1, arr2, 3);printf("%s\n", arr1);return 0; }
strncmp
![]()
int strncmp ( const char * str1, const char * str2, size_t num );
比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。
#include<stdio.h> #include<string.h> #include<assert.h> int main() {char arr1[] = "abcdef";char arr2[] = "abcq";int ret = strncmp(arr1, arr2, 4);printf("%d\n", ret);return 0; }
好啦,小雅兰今天的内容就到这里啦,还要继续加油呀!!!
相关文章:

字符函数和字符串函数(上)——“C”
各位CSDN的uu们你们好呀,今天小雅兰来给大家介绍一个全新的知识点,就是字符函数和字符串函数啦,其实其中有些函数我之前已经学习过了,比如strlen、strcpy;也有一些之前不是很熟悉的函数,比如strstr、strtok…...

九龙证券|下周解禁市值超400亿元,3股解禁压力较大
下周3股解禁比例超50%。 百利电气昨日盘中直线拉升封板,至此,百利电气两连板,累计涨幅20.85%。 昨日晚间,百利电气发布股票交易反常动摇公告称,公司不触及“室温超导”相关业务,也未打开相关研发和投入。公…...

一个大型网站架构的演变历程
正序: Rome was not built in a day(罗马不是一天建成的。)一个成熟的大型网站从来都不是一蹴而就的,需要经过多次架构的调整和升级,我们熟知的大型网站比如京东、淘宝、亚马逊,它们每天都有巨大的用户访问…...

前端前沿web 3d可视化技术 ThreeJS学习全记录
前端前沿web 3d可视化技术 随着浏览器性能和网络带宽的提升 使得3D技术不再是桌面的专利 打破传统平面展示模式 前端方向主要流向的3D图形库包括Three.js和WebGL WebGL灵活高性能,但代码量大,难度大,需要掌握很多底层知识和数学知识 Threej…...

链表经典笔试题(LeetCode刷题)
本篇文章主要是对力扣和牛客网上一些经典的和链表有关的笔试题的总结归纳,希望对你有所帮助。 目录 一、移除链表元素 1.1 问题描述 1.2 思路一 1.2.1 分析 1.2.2 代码 1.3 思路二 1.3.1 分析 1.2.3 思路三 1.3 代码实现 1.3.1 思路1的代码 1.3.2 思路2的…...

SpringCloud五大组件
微服务SpringCloud整合技术组件基本流程: 引入组件启动器依赖坐标覆盖默认配置即application.properties配置文件(每个微服务只有一个并且服务启动默认加载)引导类(微服务入口即main方法)自定义开启组件注解 SpringCloudEureka 服务注册中心,分为Eure…...

Echart的使用初体验,Echarts的基本使用及语法格式,简单图表绘制和使用及图例添加【学习笔记】
Echart? ECharts 是一个使用 JavaScript 实现的开源可视化库,涵盖各行业图表,满足各种需求。 ECharts 遵循 Apache-2.0 开源协议,免费商用。 ECharts 兼容当前绝大部分浏览器(IE8/9/10/11,Chrome…...

聊聊腾讯T13技术专家被开除
这两天腾讯的技术大佬stonehuang被曝离开腾讯,据他老婆在小红书上发的帖子称是遭遇了裁员,说实话刚看到这个消息我挺震惊的,stonehuang在中国大前端领域是排得上号的专家,同时他2005年就加入了腾讯,在qq空间的发展历程…...

c++ 常见宏、模板用法【1】
目录1、宏定义实现简单的断言2、可变参数模板3、变量模板4、宏定义实现范围内的for循环5、模板实现函数对象6、宏定义实现作用域限定7、类型萃取模板1、宏定义实现简单的断言 #define ASSERT(expr) \if(!(expr)) { \std::cout << "assertion failed: " <&l…...

【25】Verilog进阶 - 序列检测
VL25 输入序列连续的序列检测 本题并不难【中等】难度给高了 【做题关键】 (1)需要使用移位寄存器的思路。其实reg型是寄存器,也可以当做是移位寄存器,重要的是对其的处理,使用的是移位寄存器的思路 (2)注意新移入数据存放在低位 1 题目 + 代码 + TestBench 很简单,没…...

如何绕开运营商的 QoS 限制
运营商针对 UDP 进行限制,这是 QUIC 以及类似 UDP-Based 协议的推广阻力之一,上了线很多问题,丢包,慢等的问题严重增加运维,运营成本。 按照运营商五元组 QoS 这种简单粗暴不惹事的原则,只要换一个端口就可…...

C#基础教程22 异常处理
文章目录 C# 异常处理语法C# 中的异常类异常类 描述异常处理创建用户自定义异常C# 异常处理 异常是在程序执行期间出现的问题。C# 中的异常是对程序运行时出现的特殊情况的一种响应,比如尝试除以零。 异常提供了一种把程序控制权从某个部分转移到另一个部分的方式。C# 异常处理…...

java八股文--java基础
java基础1.什么是面向对象,谈谈对面向对象的理解2.JDK JRE JVM的区别与联系3.和equals4.hashCode与equals5.String StringBuffer StringBuilder的区别6.重载和重写的区别7.接口和抽象类8.List和Set的区别9.ArrayList和LinkedList10.HashMap和HashTable的区别&#x…...

2022年全国职业院校技能大赛(中职组)网络安全竞赛试题A模块第四套解析(详细)
2022年全国职业院校技能大赛(中职组) 网络安全竞赛试题 (4) (总分100分) 赛题说明 一、竞赛项目简介 “网络安全”竞赛共分A.基础设施设置与安全加固;B.网络安全事件响应、数字取证调查和应用安全;C.CTF夺旗-攻击;D.CTF夺旗-防御等四个模块。根据比赛实际情况,竞…...

【Spark】spark使用jdbc连接带有kerberos认证的hive jdbc
背景 这个需求就是spark不通过spark-hive的方式访问hive数据,而是通过spark读取hive jdbc的方式访问hive数据,因为这个hive有kerberos认证,在网上也不是很容易搜索到这样的操作案例。不多bb,直接上教程。 准备工作 准备一个hiv…...

【Maven】项目中pom.xml坐标定义以及pom基本配置
目录 一、pom.xml坐标定义 二、pom 基本配置 一、pom.xml坐标定义 在 pom.xml 中定义坐标,内容包括:groupId、artifactId、version,详细内容如下: <!--项目名称,定义为组织名项目名,类似包名-->&l…...

Linux GCC 编译详解
文章目录一、GCC 编译器简介二、GCC 工作流编程语言的发展GCC 工作流程gcc 和 g 的区别三、使用 GCC 编译GCC 编译格式GCC 编译流程多个源文件编译一、GCC 编译器简介 首先,什么是编译器呢? 我们可以使用编辑器(如 linux 下的 vi、windows 下…...

谁说程序员不懂了浪费,女神节安排
Python的PyQt框架的使用一、前言二、女神节文案三、浪漫的代码四、官宣文案一、前言 个人主页: ζ小菜鸡大家好,我是ζ小菜鸡,特在这个特殊的日子献上此文,希望小伙伴们能讨自己的女神欢心。 二、女神节文案 1.生活一半是柴米油盐,…...

上市公司管理层短视指标(2007-2020)
1、数据说明:将研发⽀出的减少量(∆R&D)作为管理层短视⾏为的度量指标,即∆R&D为公司t年的研发⽀出减去t-1年的研发⽀出并除以t-1年末的总资产再乘以100。2、数据来源:自主整理3、时间跨度:2007-20…...

IDDPM 和 DDIM 对比
IDDPM 和 DDPM 对比IDDPMDDIMIDDPM IDDPM:Improved Denoising diffusion probabilistic models learning Σθ\Sigma_{\theta}Σθ, 即Σθ(xt,t)exp(vlogβt(1−v)logβ~t)\Sigma_{\theta}\left(x_{t}, t\right)\exp \left(v \log \beta_{t}(1…...

链表OJ题(上)
✅每日一练:876. 链表的中间结点 - 力扣(LeetCode) 解题思路: 定义快慢指针,让快指针走2步,慢指针走1步,当fast或者fast.next为空时,走完链表,此时slow就是中间位置 pub…...

【题解】百度2021校招Web前端工程师笔试卷(第一批):单选题、多选题
题目来源:牛客网公司真题_免费模拟题库_企业面试|笔试真题 (nowcoder.com) 若有错误请指正! 单选题 1 某主机的 IP 地址为 212.212.77.55,子网掩码为 255.255.252.0。若该主机向其所在子网发送广播分组,则目的地址可以是&…...

论文解读:SuperPoint: Self-Supervised Interest Point Detection and Description
发表时间: 2018年 项目地址:https://arxiv.org/abs/1712.07629 论文地址:https://github.com/magicleap/SuperPointPretrainedNetwork 本文提出了一种用于训练计算机视觉中大量多视点几何问题的兴趣点检测器和描述符的自监督框架。与patch-based的神经网…...

游戏玩的多,陪玩你了解的多吗?用Python来采集陪玩数据,看看行情和美照
前言 (。・∀・)ノ゙嗨 大家好 现在应该每个人都玩过游戏吧,有些的上瘾,天天玩停不下来,有些的倒是没啥感觉 有游戏就肯定有陪玩啊,毕竟当朋友忙的时候,自己一个…...

React框架创建项目详细流程-项目的基本配置-项目的代码规范
文章目录React创建项目流程与规范项目规范项目配置目录结构样式重置Router配置Redux状态管理axios配置React创建项目流程与规范 项目规范 项目规范: 在项目中都会有一些开发规范和代码风格, 下面介绍一下我采用的规范与风格 文件夹、文件名称统一小写、多个单词以连接符(-)连…...

nnunet入门之一 (CT图像分割)
目录安装环境数据处理预处理训练测试MIC-DKFZ/nnUNet 选择Linux环境运行该项目,Windows环境需要更改较多的参数,暂不支持。 安装环境 安装cuda, cudnn,已安装的检测cuda版本 检测cuda版本: nvcc -v cd /usr/local nvidia-smi&…...

从0到1_批量下载视频
简介:真实从0到1,童叟无欺~ 目标:用python批量下载搜索视频,以“CG 服装”为例 搜索图片就不放啦,不能过审 本章主要介绍如何用python把搜索到的视频直接下载到自己的本地文件夹中~ 介绍一下工作…...

CNCF x Alibaba云原生技术公开课 第十二章 可观测性:监控与日志
1、监控 监控类型 资源监控:cpu、内存、网络等。性能监控:apm监控,一般是通过一些 Hook 的机制在,在虚拟机层、字节码执行层通过隐式调用,或者是在应用层显示注入,获取更深层次的一个监控指标,…...

C语言宏定义几个问题
1.#define Ant A虽说做的是将代码中Ant替换成A,但是是整体的替换,不能将整体分离替换。 不带宏参定义一般形式如下: 格式: #define 标识符 字符串 其中“标识符”为所定义的宏名,“字符串”可以是常数、表达式、格式串…...

王道计算机组成原理课代表 - 考研计算机 第二章 数据的表示和运算 究极精华总结笔记
本篇博客是考研期间学习王道课程 传送门 的笔记,以及一整年里对 计算机组成 知识点的理解的总结。希望对新一届的计算机考研人提供帮助!!! 关于对 数据的表示和运算 章节知识点总结的十分全面,涵括了《计算机组成原理…...