[知识点]C 标准库常用字符串处理函数

C 标准库提供了许多字符串处理函数,可以用于操作 C 风格的字符串(以 \0 结尾的字符数组)。

strcpystrncpy

strcpy

strcpy 将源字符串复制到目标字符串,包含终止符 \0

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    char source[] = "hello world";
    char destination[50];

    strcpy(destination, source);
    std::cout << "Destination: " << destination << std::endl;

    return 0;
}
  • 优点:简单、直观。
  • 缺点 :如果目标数组长度不足,会导致缓冲区溢出。
strncpy

strncpystrcpy安全版本 ,它会复制最多 n 个字符,并确保目标数组不会溢出。如果源字符串长度小于 n,目标字符串会被填充 \0

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    char source[] = "hello world";
    char destination[50];

    strncpy(destination, source, sizeof(destination) - 1);
    destination[sizeof(destination) - 1] = '\0'; // 确保以 '\0' 结尾

    std::cout << "Destination: " << destination << std::endl;

    return 0;
}
  • 优点:可以防止缓冲区溢出。
  • 缺点需要手动添加终止符

strlen

strlen 用于计算字符串的长度, 括终止符 \0

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    const char* str = "hello world";
    size_t length = strlen(str);

    std::cout << "Length of string: " << length << std::endl;

    return 0;
}
  • 优点:简单、快速。
  • 缺点如果传入的不是以 \0 结尾的字符串,会导致未定义行为。

strcmpstrncmp

strcmp

strcmp 比较两个字符串,返回值:

  • 0:两个字符串相等
  • 小于0:第一个字符串小于第二个字符串
  • 大于0:第一个字符串大于第二个字符串
cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    const char* str1 = "hello";
    const char* str2 = "world";

    int result = strcmp(str1, str2);
    if (result == 0) {
        std::cout << "Strings are equal." << std::endl;
    } else if (result < 0) {
        std::cout << "str1 is less than str2." << std::endl;
    } else {
        std::cout << "str1 is greater than str2." << std::endl;
    }

    return 0;
}
strncmp

strncmp 比较两个字符串的前 n 个字符。

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    const char* str1 = "hello";
    const char* str2 = "heaven";

    int result = strncmp(str1, str2, 3);
    if (result == 0) {
        std::cout << "First 3 characters are equal." << std::endl;
    } else if (result < 0) {
        std::cout << "str1 is less than str2 in the first 3 characters." << std::endl;
    } else {
        std::cout << "str1 is greater than str2 in the first 3 characters." << std::endl;
    }

    return 0;
}

strcatstrncat

strcat

strcat 将源字符串追加到目标字符串的末尾,目标字符串必须有足够的空间来容纳追加的内容。

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    char destination[50] = "Hello";
    const char* source = " world";

    strcat(destination, source);
    std::cout << "Concatenated string: " << destination << std::endl;

    return 0;
}
  • 优点:简单、方便。
  • 缺点:如果目标数组长度不足,会导致缓冲区溢出。
strncat

strncat 将源字符串的前 n 个字符追加到目标字符串的末尾。

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    char destination[50] = "Hello";
    const char* source = " world";

    strncat(destination, source, 3);
    std::cout << "Concatenated string: " << destination << std::endl;

    return 0;
}
  • 优点:可以防止缓冲区溢出。
  • 缺点:需要确保目标数组有足够的空间。

其他常用的字符串处理函数

strchr

strchr 查找字符串中第一次出现的指定字符。

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    const char* str = "hello world";
    char ch = 'o';

    char* result = strchr(str, ch);
    if (result != NULL) {
        std::cout << "Found character '" << ch << "' at position: " << (result - str) << std::endl;
    } else {
        std::cout << "Character not found." << std::endl;
    }

    return 0;
}
strrchr

strrchr 查找字符串中最后一次出现的指定字符。

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    const char* str = "hello world";
    char ch = 'o';

    char* result = strrchr(str, ch);
    if (result != NULL) {
        std::cout << "Found character '" << ch << "' at position: " << (result - str) << std::endl;
    } else {
        std::cout << "Character not found." << std::endl;
    }

    return 0;
}
strstr

strstr 查找字符串中第一次出现的指定子字符串。

cpp 复制代码
#include <iostream>
#include <cstring>

int main() {
    const char* str = "hello world";
    const char* substr = "world";

    char* result = strstr(str, substr);
    if (result != NULL) {
        std::cout << "Found substring \"" << substr << "\" at position: " << (result - str) << std::endl;
    } else {
        std::cout << "Substring not found." << std::endl;
    }

    return 0;
}

总结

这些字符串处理函数在 C 和 C++ 中都非常有用,但在使用它们时需要特别注意缓冲区溢出和未定义行为。

相关推荐
sp_fyf_2024几秒前
计算机前沿技术-人工智能算法-大语言模型-最新论文阅读-2024-09-15
论文阅读·人工智能·算法·搜索引擎·语言模型·自然语言处理
2401_857297911 小时前
招联金融内推(深圳武汉大量招后端、算法)---2025秋招内推
java·前端·算法·金融·求职招聘
非常规定义M1 小时前
Day30_0.1基础学习MATLAB学习小技巧总结(30)——优化工具箱及
开发语言·人工智能·学习·算法·数学建模·matlab
柠檬少少开发1 小时前
碎纸片的自动拼接复原技术
人工智能·算法·计算机视觉
2401_857297912 小时前
招联金融秋招内推喇--18薪
java·前端·算法·金融·求职招聘
larryyu_cs2 小时前
CF1494F Delete The Edges 题解
c++·算法·图论
王俊山IT2 小时前
C++学习笔记----7、使用类与对象获得高性能(二)---- 理解对象生命周期(7)
开发语言·c++·笔记·学习
BUG制造机.3 小时前
计算机网络 --- Socket 编程
服务器·c++·计算机网络
城南vision3 小时前
算法题总结(三)——滑动窗口
数据结构·算法
Jurio.3 小时前
【JPCS出版】第二届应用统计、建模与先进算法国际学术会议(ASMA2024,9月27日-29)
大数据·人工智能·深度学习·算法·机器学习·数学建模