当前位置: 首页 > news >正文

C标准库<string.h>-str、strn开头的函数

char *strcat(char *dest, const char *src)

函数功能

strcat 函数用于将一个字符串追加到另一个字符串的尾部。

参数解释

  • dest:指向目标字符串的指针,这个字符串的尾部将被追加 src 字符串的内容。
  • src:指向源字符串的指针,其内容将被追加到 dest 字符串的尾部。

返回值

  • 返回指向目标字符串 dest 的指针。

函数实现

函数用法

#include <stdio.h>
#include <string.h>int main() {char dest[50] = "Hello,";const char *src = "World!";// 将src追加到dest的尾部strcat(dest, src);printf("Concatenated String: '%s'\n", dest);return 0;
}
Concatenated String: 'Hello, World!'

 char *strncat(char *dest, const char *src, size_t n) 

函数功能

strncat 函数用于将一个字符串的一部分追加到另一个字符串的尾部,最多追加指定的字符数。

参数解释

  • dest:指向目标字符串的指针,这个字符串的尾部将被追加 src 字符串的内容。
  • src:指向源字符串的指针,其内容将被追加到 dest 字符串的尾部。
  • n:最大的字符追加数,包括空字符。

返回值

  • 返回指向目标字符串 dest 的指针。

函数实现

函数用法

#include <stdio.h>
#include <string.h>int main() {char dest[50] = "Hello, ";char *src = "World! This is a test.";// 将src的前14个字符追加到dest的尾部strncat(dest, src, 14);printf("Concatenated String: '%s'\n", dest);return 0;
}
Concatenated String: 'Hello, World! This is'
请按任意键继续. . .

char *strchr(const char *str, int c) 

函数功能

strchr 函数用于在一个字符串中查找第一次出现的指定字符。

参数解释

  • str:指向要搜索的字符串的指针。
  • c:要搜索的字符(以整数形式给出)。

返回值

  • 如果找到了字符 c,则返回指向找到的第一个匹配字符的指针。
  • 如果没有找到,则返回 NULL

函数实现

函数用法

#include <stdio.h>
#include <string.h>int main() {char *str = "Hello, World!";int c = 'W';// 使用 strchr 查找字符 'W'char *result = strchr(str, c);if (result) {printf("Character '%c' found at position: %ld\n", c, result - str + 1);} else {printf("Character '%c' not found.\n", c);}return 0;
}
Character 'W' found at position: 8
请按任意键继续. . .

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

函数功能

strcmp 函数用于比较两个字符串,并根据比较结果返回一个整数

参数解释

  • str1:指向第一个要比较的字符串的指针。
  • str2:指向第二个要比较的字符串的指针。

返回值

  • 如果 str1 小于 str2,则返回负整数。
  • 如果 str1 等于 str2,则返回 0
  • 如果 str1 大于 str2,则返回正整数。

函数实现

函数用法

#include <stdio.h>
#include <string.h>int main() {const char *str1 = "Hello";const char *str2 = "Hell";const char *str3 = "Hello";int result1 = strcmp(str1, str2);int result2 = strcmp(str1, str3);if (result1 < 0) {printf("'%s' is less than '%s'\n", str1, str2);} else if (result1 > 0) {printf("'%s' is greater than '%s'\n", str1, str2);} else {printf("'%s' is equal to '%s'\n", str1, str2);}if (result2 < 0) {printf("'%s' is less than '%s'\n", str1, str3);} else if (result2 > 0) {printf("'%s' is greater than '%s'\n", str1, str3);} else {printf("'%s' is equal to '%s'\n", str1, str3);}return 0;
}
'Hello' is greater than 'Hell'
'Hello' is equal to 'Hello'
请按任意键继续. . .

下面的程序,和上面的程序有一点点不同,结果也不一样。 

#include <stdio.h>
#include <string.h>int main() {const char *str1 = "Healo";const char *str2 = "Hell";const char *str3 = "Hello";int result1 = strcmp(str1, str2);int result2 = strcmp(str1, str3);if (result1 < 0) {printf("'%s' is less than '%s'\n", str1, str2);} else if (result1 > 0) {printf("'%s' is greater than '%s'\n", str1, str2);} else {printf("'%s' is equal to '%s'\n", str1, str2);}if (result2 < 0) {printf("'%s' is less than '%s'\n", str1, str3);} else if (result2 > 0) {printf("'%s' is greater than '%s'\n", str1, str3);} else {printf("'%s' is equal to '%s'\n", str1, str3);}return 0;
}
'Healo' is less than 'Hell'
'Healo' is less than 'Hello'
请按任意键继续. . .

函数功能

参数解释

返回值

函数实现

函数用法

函数功能

参数解释

返回值

函数实现

函数用法

http://www.lryc.cn/news/446060.html

相关文章:

  • Anaconda/Miniconda的删除和安装
  • 【Harmony】轮播图特效,持续更新中。。。。
  • Go 并发模式:管道的妙用
  • CAN通信详解
  • 52 文本预处理_by《李沐:动手学深度学习v2》pytorch版
  • 【python】字符串扩展-格式化的精度控制
  • C++第一次练习
  • 计算机毕业设计 基于Python的医疗预约与诊断系统 Django+Vue 前后端分离 附源码 讲解 文档
  • JAVA基础:正则表达式,String的intern方法,StringBuilder可变字符串特点与应用,+连接字符串特点
  • 前端接口报错302 [已解决]
  • 【网络安全】利用未授权API接口实现创建Support Ticket
  • 气压高度加误差的两种方法(直接添加 vs 换算到气压误差),附MATLAB程序
  • Word 制作会议名牌教程
  • 浮动静态路由
  • JavaWeb初阶 day1
  • OpenAPI鉴权(二)jwt鉴权
  • 【Rust练习】16.模式
  • 深度学习(4):torch.nn.Module
  • (14)关于docker如何通过防火墙做策略限制
  • 新React开发人员应该如何思考
  • 解密.bixi、.baxia勒索病毒:如何安全恢复被加密数据
  • 开源 AI 智能名片与 S2B2C 商城小程序:嫁接权威实现信任与增长
  • S-Clustr-Simple 飞机大战:骇入现实的建筑灯光游戏
  • MySQL:存储引擎简介和库的基本操作
  • JavaScript类型判断(总结)
  • SpringBoot之登录校验关于JWT、Filter、interceptor、异常处理的使用
  • 我的AI工具箱Tauri版-FunAsr音频转文本
  • C++:模版初阶
  • Python Web 与区块链集成的最佳实践:智能合约、DApp与安全
  • 使用工具将截图公式转换为word公式