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

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

strcpystrncpy

strcpy

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

#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

#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

#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:第一个字符串大于第二个字符串
#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 个字符。

#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 将源字符串追加到目标字符串的末尾,目标字符串必须有足够的空间来容纳追加的内容。

#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 个字符追加到目标字符串的末尾。

#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 查找字符串中第一次出现的指定字符。

#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 查找字符串中最后一次出现的指定字符。

#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 查找字符串中第一次出现的指定子字符串。

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

相关推荐

  1. [知识]C 标准字符串处理函数

    2024-06-08 23:10:01       33 阅读
  2. C++标准函数(长期更新中)

    2024-06-08 23:10:01       23 阅读
  3. C/C++ 01」C标准中常见的字符串处理函数

    2024-06-08 23:10:01       38 阅读
  4. go语言-字符串处理函数

    2024-06-08 23:10:01       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 23:10:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 23:10:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 23:10:01       82 阅读
  4. Python语言-面向对象

    2024-06-08 23:10:01       91 阅读

热门阅读

  1. 学习分享-微服务的相关概念

    2024-06-08 23:10:01       29 阅读
  2. Mybatis面试题系列四

    2024-06-08 23:10:01       29 阅读
  3. ASP.NET第五章 Application、Session和Cookie对象

    2024-06-08 23:10:01       30 阅读
  4. Springboot整合Knife4j接口文档

    2024-06-08 23:10:01       36 阅读
  5. 12、架构-流量治理之服务容错

    2024-06-08 23:10:01       29 阅读
  6. 在Android中使用 MQTT 服务实现消息通信

    2024-06-08 23:10:01       27 阅读
  7. CSS实现渐隐渐现效果

    2024-06-08 23:10:01       28 阅读
  8. 一文搞懂 reST 和 Markdown 语法

    2024-06-08 23:10:01       31 阅读