一.strchr - 字符查找函數
1.函數原型
char *strchr(const char *str, int c);
2.核心功能
在字符串中查找特定字符的第一次出現位置
3.參數說明
參數 類型 ????說明
str ???const char* 要搜索的字符串
c ?????int ????????要查找的字符(自動轉換為char)
4.返回值
找到:返回指向該字符的指針
未找到:返回NULL
特殊:c = '\0' 時返回字符串結尾的空字符位置
5.用法示例
(1)基礎查找
#include <stdio.h>
#include <string.h>
int main() {
????const char *text = "Hello, World!";
????char *ptr = strchr(text, 'W');
????
????if(ptr) {
????????printf("找到 'W',位置: %ld\n", ptr - text); // 輸出: 7
????????printf("后續內容: %s\n", ptr); ?????????????// 輸出: World!
????} else {
????????printf("未找到字符\n");
????}
????return 0;
}
(2)查找文件擴展名
char filename[] = "document.pdf";
char *dot = strchr(filename, '.');
if(dot) {
????printf("文件擴展名: %s\n", dot + 1); // 輸出: pdf
} else {
????printf("無擴展名\n");
}
(3)統計字符出現次數
int count_char(const char *str, char c) {
????int count = 0;
????const char *ptr = str;
????
????while((ptr = strchr(ptr, c)) != NULL) {
????????count++;
????????ptr++; // 移動到下一個位置繼續搜索
????}
????return count;
}
// 使用示例
char sentence[] = "She sells seashells by the seashore";
int s_count = count_char(sentence, 's');
printf("'s'出現次數: %d\n", s_count); // 輸出: 7
二.strstr - 子串查找函數
1.函數原型
char *strstr(const char *haystack, const char *needle);
2.核心功能
在字符串中查找特定子串的第一次出現位置
3.參數說明
參數 ?類型 ????說明
haystack ?const char* 被搜索的主字符串
needle ?const char* 要查找的子字符串
4.返回值
找到:返回指向子串起始位置的指針
未找到:返回NULL
特殊:needle為空字符串時返回haystack
5.用法示例
(1)基礎子串查找
#include <stdio.h>
#include <string.h>
int main() {
????const char *text = "The quick brown fox jumps over the lazy dog";
????char *found = strstr(text, "fox");
????
????if(found) {
????????printf("找到子串,位置: %ld\n", found - text); // 輸出: 16
????????printf("后續內容: %s\n", found); ?????????????// 輸出: fox jumps over...
????} else {
????????printf("未找到子串\n");
????}
????return 0;
}
(2)檢查文件類型
int is_image_file(const char *filename) {
????const char *extensions[] = {".jpg", ".png", ".gif", ".bmp"};
????
????for(int i = 0; i < 4; i++) {
????????if(strstr(filename, extensions[i]) != NULL) {
????????????return 1; // 是圖片文件
????????}
????}
????return 0; // 不是圖片文件
}
// 使用示例
if(is_image_file("vacation.jpg")) {
????printf("這是圖片文件\n");
}
(3)提取HTML標簽內容
void extract_html_content(const char *html) {
????const char *start = strstr(html, "<title>");
????if(!start) return;
????
????start += 7; // 跳過"<title>"
????
????const char *end = strstr(start, "</title>");
????if(!end) return;
????
????// 計算標題長度
????size_t len = end - start;
????char title[len + 1];
????strncpy(title, start, len);
????title[len] = '\0';
????
????printf("頁面標題: %s\n", title);
}
// 使用示例
extract_html_content("<html><title>Welcome Page</title></html>");
// 輸出: 頁面標題: Welcome Page
三.兩函數對比分析
四.實際應用場景
1.命令行參數解析
int main(int argc, char *argv[]) {
????int verbose = 0;
????char *output_file = NULL;
????
????for(int i = 1; i < argc; i++) {
????????if(strstr(argv[i], "--help")) {
????????????print_help();
????????????return 0;
????????}
????????else if(strstr(argv[i], "--verbose")) {
????????????verbose = 1;
????????}
????????else if(strchr(argv[i], '=')) {
????????????parse_key_value(argv[i]);
????????}
????}
????// ...
}
2.日志文件分析
void analyze_log(const char *logfile) {
????FILE *file = fopen(logfile, "r");
????if(!file) return;
????
????char line[256];
????int error_count = 0, warning_count = 0;
????
????while(fgets(line, sizeof(line), file)) {
????????if(strstr(line, "[ERROR]")) error_count++;
????????else if(strstr(line, "[WARNING]")) warning_count++;
????}
????
????printf("錯誤數: %d\n警告數: %d\n", error_count, warning_count);
????fclose(file);
}