下面是每个C语言字符串操作函数的单独示例代码,包含详细注释和必要的头文件:
#include <stdio.h>
#include <string.h> // 字符串操作函数头文件
#include <stdlib.h> // 用于malloc
int main() {
// 1. strlen - 获取字符串长度
const char *s1 = "Hello";
size_t len = strlen(s1);
printf("1. strlen(\"%s\") = %zu\n\n", s1, len); // 5
// 2. strcpy - 字符串复制
char dest1[20];
strcpy(dest1, "Hello World!");
printf("2. strcpy result: \"%s\"\n\n", dest1);
// 3. strncpy - 安全字符串复制
char dest2[10];
strncpy(dest2, "OverflowTest", sizeof(dest2)); // 只复制前9个字符
dest2[sizeof(dest2) - 1] = '\0'; // 确保结束符
printf("3. strncpy result: \"%s\"\n\n", dest2);
// 4. strcat - 字符串连接
char dest3[20] = "Hello";
strcat(dest3, " World!");
printf("4. strcat result: \"%s\"\n\n", dest3);
// 5. strncat - 安全字符串连接
char dest4[20] = "Secure";
strncat(dest4, " Append!!!", 7); // 只追加7个字符
printf("5. strncat result: \"%s\"\n\n", dest4);
// 6. strcmp - 字符串比较
int cmp1 = strcmp("apple", "banana");
int cmp2 = strcmp("banana", "apple");
int cmp3 = strcmp("apple", "apple");
printf("6. strcmp:\n");
printf(" \"apple\" vs \"banana\": %d\n", cmp1); // <0
printf(" \"banana\" vs \"apple\": %d\n", cmp2); // >0
printf(" \"apple\" vs \"apple\": %d\n\n", cmp3); // 0
// 7. strncmp - 安全字符串比较
int cmp4 = strncmp("application", "apple", 3);
printf("7. strncmp(\"application\", \"apple\", 3): %d (前3字符相同)\n\n", cmp4);
// 8. strchr - 查找字符首次出现
const char *str8 = "Hello World";
char *found = strchr(str8, 'o');
printf("8. strchr(\"%s\", 'o') found at position: %ld (\"%s\")\n\n",
str8, found - str8, found);
// 9. strrchr - 查找字符最后一次出现
char *last = strrchr(str8, 'o');
printf("9. strrchr(\"%s\", 'o') found at position: %ld (\"%s\")\n\n",
str8, last - str8, last);
// 10. strstr - 查找子串
const char *text = "C programming is powerful";
char *sub = strstr(text, "program");
printf("10. strstr(\"%s\", \"program\") found at: \"%s\"\n\n", text, sub);
// 11. strtok - 字符串分割
char data[] = "John,Doe,25,USA"; // 必须可修改
printf("11. strtok splitting \"%s\":\n", data);
char *token = strtok(data, ",");
while(token != NULL) {
printf(" - %s\n", token);
token = strtok(NULL, ",");
}
printf("\n");
// 12. memcpy - 内存复制
char src[50] = "Memory copy example";
char dest5[50];
memcpy(dest5, src, strlen(src) + 1); // +1包含结束符
printf("12. memcpy result: \"%s\"\n\n", dest5);
// 13. memmove - 处理重叠的内存复制
char str13[] = "memmove can handle overlap";
printf("13. Before memmove: \"%s\"\n", str13);
memmove(str13 + 4, str13, 10); // 从开头复制10字节到位置4
printf(" After memmove: \"%s\"\n\n", str13);
// 14. memset - 内存设置
char buf[20];
memset(buf, 'A', 10); // 设置10字节为'A'
buf[10] = '\0'; // 结束符
printf("14. memset result: \"%s\"\n", buf);
return 0;
}
示例输出:
1. strlen("Hello") = 5
2. strcpy result: "Hello World!"
3. strncpy result: "OverflowT"
4. strcat result: "Hello World!"
5. strncat result: "Secure Appen"
6. strcmp:
"apple" vs "banana": -1
"banana" vs "apple": 1
"apple" vs "apple": 0
7. strncmp("application", "apple", 3): 0 (前3字符相同)
8. strchr("Hello World", 'o') found at position: 4 ("o World")
9. strrchr("Hello World", 'o') found at position: 7 ("orld")
10. strstr("C programming is powerful", "program") found at: "programming is powerful"
11. strtok splitting "John,Doe,25,USA":
- John
- Doe
- 25
- USA
12. memcpy result: "Memory copy example"
13. Before memmove: "memmove can handle overlap"
After memmove: "memmemmove can handle overlap"
14. memset result: "AAAAAAAAAA"
关键注意事项:
-
缓冲区安全:
strncpy
/strncat
需手动添加结束符(如示例3)- 目标数组必须足够大(如示例2、3、4使用固定大小的数组)
-
字符串修改:
strtok
会修改原字符串(示例11使用char[]
而非char*
)- 函数如
strchr
、strstr
返回的是原始字符串的指针
-
内存操作:
memmove
可处理内存重叠(示例13)memset
常用于初始化缓冲区(示例14)
-
返回值处理:
- 比较函数返回值的正负表示大小关系
- 查找函数返回NULL表示未找到
在实际使用时,请务必:
- 检查目标缓冲区大小
- 处理可能的返回值(如查找失败)
- 优先使用
strn
系列安全函数 - 区分字符串常量和可修改字符串