字符串复制,拼接,比较大小函数

1. 字符串比较大小函数:strcmp():

这是一个字符串比较函数,作用是比较两个字符串。函数原型:

cpp 复制代码
int strcmp(const char *str1, const char *str2)

其中 str1 和 str2 是要进行比较的字符串。

如果 str1 < str2,则返回值 < 0;

如果 str1`== str2,则返回值 == 0;

如果 str1 > str2,则返回值 > 0。

字符串比较是按照 ASCII 值进行的。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    
    int result = strcmp(str1, str2);
    
    if (result < 0) {
        printf("str1 is less than str2\n");
    } else if (result == 0) {
        printf("str1 is equal to str2\n");
    } else {
        printf("str1 is greater than str2\n");
    }
    
    return 0;
}

输出结果:

cpp 复制代码
str1 is less than str2

2. 字符串复制函数strcpy():

这是一个字符串复制函数,作用是将源字符串(含有'\0')复制到目标字符串中。函数原型:

cpp 复制代码
char *strcpy(char *dest, const char *src)

,其中 dest 和 src 分别是目标字符串和源字符串。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello, World!";
    char dest[50];
    
    strcpy(dest, src);
    
    printf("Source string : %s\n", src);
    printf("Destination string : %s\n", dest);
    
    return 0;
}

输出结果:

cpp 复制代码
Source string : Hello, World!
Destination string : Hello, World!

3. 字符串拼接函数strcat():

这是一个字符串连接函数,作用是将源字符串(除'\0'外)追加到目标字符串后,返回目标字符串。函数原型:

cpp 复制代码
char *strcat(char *dest, const char *src)

,其中 dest和 src分别是目标字符串和源字符串。

cpp 复制代码
#include <stdio.h>
#include <string.h>

int main() {
    char dest[50] = "Hello, ";
    char src[] = "World!";
    
    strcat(dest, src);
    
    printf("After concatenation: %s\n", dest);
    
    return 0;
}

输出结果:

cpp 复制代码
After concatenation: Hello,World!
相关推荐
贾全16 分钟前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天31 分钟前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
满分观察网友z1 小时前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
森焱森2 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天2 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid
闪电麦坤953 小时前
数据结构:二维数组(2D Arrays)
数据结构·算法
凌肖战3 小时前
力扣网C语言编程题:快慢指针来解决 “寻找重复数”
c语言·算法·leetcode
埃菲尔铁塔_CV算法3 小时前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉
NAGNIP4 小时前
一文搞懂FlashAttention怎么提升速度的?
人工智能·算法