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

作者 : admin 本文共2957个字,预计阅读时间需要8分钟 发布时间: 2024-06-5 共4人阅读

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

strcpystrncpy

strcpy

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

#include 
#include 

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 
#include 

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

    strncpy(destination, source, sizeof(destination) - 1);
    destination[sizeof(destination) - 1] = '
#include 
#include 
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;
}
'
; // 确保以 '
#include 
#include 
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;
}
' 结尾
std::cout << "Destination: " << destination << std::endl; return 0; }
  • 优点:可以防止缓冲区溢出。
  • 缺点需要手动添加终止符

strlen

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

#include 
#include 

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 
#include 

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 
#include 

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 
#include 

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 
#include 

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 
#include 

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 
#include 

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 
#include 

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

本站无任何商业行为
个人在线分享 » [知识点]C 标准库常用字符串处理函数
E-->