鱼C论坛

 找回密码
 立即注册
楼主: bgc1314159

[已解决]复合字面量

[复制链接]
 楼主| 发表于 昨天 19:47 From FishC Mobile | 显示全部楼层
FishC 发表于 2025-11-25 10:52
# strncpy 的安全使用模式

你的思考很深入!确实,从安全编程的角度来看,**总是假设源字符串可能大 ...

strstr,stpbrk,strchr
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 19:47 | 显示全部楼层
bgc1314159 发表于 2025-11-25 19:47
FishC 发表于 2025-11-25 10:52
# strncpy 的安全使 ...


# strstr, strpbrk, strchr 函数详解

这三个都是C语言中的字符串查找函数,但功能有所不同:

## 1. strstr - 查找子字符串

### 函数原型
  1. char *strstr(const char *haystack, const char *needle);
复制代码


### 功能
在 `haystack` 字符串中查找 `needle` 子字符串的第一次出现位置。

### 示例
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main() {
  4.     char text[] = "Hello, welcome to FishC!";
  5.     char *result;
  6.    
  7.     result = strstr(text, "welcome");
  8.     if (result != NULL) {
  9.         printf("找到子串: %s\n", result);  // 输出: welcome to FishC!
  10.         printf("位置: %ld\n", result - text);  // 输出: 7
  11.     } else {
  12.         printf("未找到子串\n");
  13.     }
  14.    
  15.     return 0;
  16. }
复制代码


## 2. strpbrk - 查找字符集合中的任意字符

### 函数原型
  1. char *strpbrk(const char *str, const char *accept);
复制代码


### 功能
在 `str` 字符串中查找 `accept` 字符集合中任意字符的第一次出现位置。

### 示例
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main() {
  4.     char text[] = "Hello, world! 123";
  5.     char *result;
  6.    
  7.     // 查找数字字符
  8.     result = strpbrk(text, "0123456789");
  9.     if (result != NULL) {
  10.         printf("找到数字: %s\n", result);  // 输出: 123
  11.         printf("位置: %ld\n", result - text);  // 输出: 14
  12.     }
  13.    
  14.     // 查找标点符号
  15.     result = strpbrk(text, ",.!");
  16.     if (result != NULL) {
  17.         printf("找到标点: %s\n", result);  // 输出: , world! 123
  18.     }
  19.    
  20.     return 0;
  21. }
复制代码


## 3. strchr - 查找特定字符

### 函数原型
  1. char *strchr(const char *str, int c);
复制代码


### 功能
在 `str` 字符串中查找字符 `c` 的第一次出现位置。

### 示例
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main() {
  4.     char text[] = "Hello, world!";
  5.     char *result;
  6.    
  7.     // 查找逗号
  8.     result = strchr(text, ',');
  9.     if (result != NULL) {
  10.         printf("找到逗号: %s\n", result);  // 输出: , world!
  11.         printf("位置: %ld\n", result - text);  // 输出: 5
  12.     }
  13.    
  14.     // 查找字符 'o'
  15.     result = strchr(text, 'o');
  16.     if (result != NULL) {
  17.         printf("第一个 'o': %s\n", result);  // 输出: o, world!
  18.     }
  19.    
  20.     return 0;
  21. }
复制代码


## 综合对比示例

  1. #include <stdio.h>
  2. #include <string.h>

  3. int main() {
  4.     char text[] = "The quick brown fox jumps over the lazy dog";
  5.    
  6.     printf("原始字符串: %s\n\n", text);
  7.    
  8.     // 使用 strstr 查找单词
  9.     char *substr = strstr(text, "brown");
  10.     printf("strstr("brown"): %s\n", substr ? substr : "未找到");
  11.    
  12.     // 使用 strpbrk 查找元音字母
  13.     char *vowel = strpbrk(text, "aeiouAEIOU");
  14.     printf("strpbrk(元音): %s\n", vowel ? vowel : "未找到");
  15.    
  16.     // 使用 strchr 查找空格
  17.     char *space = strchr(text, ' ');
  18.     printf("strchr(' '): %s\n", space ? space : "未找到");
  19.    
  20.     return 0;
  21. }
复制代码


## 实际应用场景

### 1. 解析文件路径
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main() {
  4.     char path[] = "/home/user/documents/file.txt";
  5.    
  6.     // 提取文件名
  7.     char *filename = strrchr(path, '/');  // 从右边查找
  8.     if (filename != NULL) {
  9.         printf("文件名: %s\n", filename + 1);  // 跳过 '/'
  10.     }
  11.    
  12.     // 提取文件扩展名
  13.     char *extension = strrchr(path, '.');
  14.     if (extension != NULL) {
  15.         printf("扩展名: %s\n", extension + 1);  // 跳过 '.'
  16.     }
  17.    
  18.     return 0;
  19. }
复制代码


### 2. 验证字符串格式
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>

  4. bool is_valid_email(const char *email) {
  5.     // 简单邮箱格式验证
  6.     return (strchr(email, '@') != NULL) &&
  7.            (strchr(email, '.') != NULL);
  8. }

  9. int main() {
  10.     char email1[] = "user@example.com";
  11.     char email2[] = "invalid-email";
  12.    
  13.     printf("%s: %s\n", email1, is_valid_email(email1) ? "有效" : "无效");
  14.     printf("%s: %s\n", email2, is_valid_email(email2) ? "有效" : "无效");
  15.    
  16.     return 0;
  17. }
复制代码


## 总结

| 函数 | 功能 | 返回值 |
|------|------|--------|
| `strstr` | 查找子字符串 | 子字符串首次出现的位置 |
| `strpbrk` | 查找字符集合中的任意字符 | 匹配字符首次出现的位置 |
| `strchr` | 查找特定字符 | 字符首次出现的位置 |

**共同特点:**
- 都返回指向找到位置的指针
- 如果没找到,返回 `NULL`
- 都是区分大小写的
- 都在 `string.h` 头文件中声明

这些函数在处理字符串搜索和解析时非常有用,可以根据具体需求选择合适的函数。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-26 05:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表