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

相关推荐
杨福瑞11 分钟前
C语言⽂件操作讲解(总)
c语言·开发语言
Samsong41 分钟前
《C++ Primer Plus》读书笔记 第二章 开始学习C++
c++·后端
Rubisco..1 小时前
牛客周赛 Round 111
数据结构·c++·算法
兮山与1 小时前
算法8.0
算法
高山上有一只小老虎1 小时前
杨辉三角的变形
java·算法
Swift社区1 小时前
LeetCode 395 - 至少有 K 个重复字符的最长子串
算法·leetcode·职场和发展
hz_zhangrl1 小时前
CCF-GESP 等级考试 2025年9月认证C++四级真题解析
开发语言·c++·算法·程序设计·gesp·c++四级·gesp2025年9月
火山灿火山1 小时前
详解AVL树旋转操作实现
数据结构·c++
少许极端2 小时前
算法奇妙屋(六)-哈希表
java·数据结构·算法·哈希算法·散列表·排序
羊羊小栈2 小时前
基于「多模态大模型 + BGE向量检索增强RAG」的新能源汽车故障诊断智能问答系统(vue+flask+AI算法)
vue.js·人工智能·算法·flask·汽车·毕业设计·大作业