[知识点]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++ 中都非常有用,但在使用它们时需要特别注意缓冲区溢出和未定义行为。

相关推荐
地平线开发者2 分钟前
ReID/OSNet 算法模型量化转换实践
算法·自动驾驶
快乐的划水a7 分钟前
组合模式及优化
c++·设计模式·组合模式
地平线开发者16 分钟前
开发者说|EmbodiedGen:为具身智能打造可交互3D世界生成引擎
算法·自动驾驶
星星火柴9361 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
艾莉丝努力练剑2 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
C++、Java和Python的菜鸟4 小时前
第六章 统计初步
算法·机器学习·概率论
Cx330❀4 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
散1124 小时前
01数据结构-Prim算法
数据结构·算法·图论
起个昵称吧4 小时前
线程相关编程、线程间通信、互斥锁
linux·算法
阿巴~阿巴~4 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list