C语言写一个查找字符串的函数接口
时间: 2023-02-27 09:29:11 浏览: 67
下面是一个示例的 C 语言函数接口来查找字符串:
```
int find_string(char *haystack, char *needle)
{
int i, j;
int haystack_len = strlen(haystack);
int needle_len = strlen(needle);
for (i = 0; i < haystack_len - needle_len + 1; i++) {
for (j = 0; j < needle_len; j++) {
if (haystack[i + j] != needle[j])
break;
}
if (j == needle_len)
return i;
}
return -1;
}
```
该函数接收两个字符串参数:haystack 和 needle。它在 haystack 中查找 needle,并返回 needle 第一次出现在 haystack 中的位置。如果找不到 needle,则返回 -1。
相关问题
用c语言写一个接口函数,能够搜索txt文件的内容,并使用链表
以下是一个简单的示例代码,用于搜索txt文件中是否包含特定字符串,并使用链表存储搜索结果:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 1024
// 链表结构体
typedef struct node {
char *line;
struct node *next;
} Node;
// 将搜索结果添加到链表中
void add_result(Node **head, char *line) {
// 创建新节点
Node *new_node = (Node *) malloc(sizeof(Node));
new_node->line = strdup(line);
new_node->next = NULL;
// 如果链表为空,则新节点为头节点
if (*head == NULL) {
*head = new_node;
return;
}
// 否则遍历链表,找到最后一个节点,添加新节点
Node *cur = *head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = new_node;
}
// 搜索txt文件,返回链表
Node *search_txt(char *filename, char *target) {
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
Node *head = NULL;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Unable to open %s\n", filename);
return NULL;
}
// 逐行读取文件,查找是否包含目标字符串
while ((read = getline(&line, &len, fp)) != -1) {
if (strstr(line, target) != NULL) {
add_result(&head, line);
}
}
fclose(fp);
if (line) {
free(line);
}
return head;
}
// 打印链表内容
void print_results(Node *head) {
Node *cur = head;
while (cur != NULL) {
printf("%s", cur->line);
cur = cur->next;
}
}
int main() {
char *filename = "test.txt";
char *target = "apple";
Node *results = search_txt(filename, target);
if (results != NULL) {
printf("Search results:\n");
print_results(results);
} else {
printf("No results found.\n");
}
return 0;
}
```
在此示例代码中,我们首先定义了一个链表结构体 `Node`,它包含一个字符串指针 `line` 和一个指向下一个节点的指针 `next`。接着定义了一个 `add_result` 函数,用于将搜索结果添加到链表中。
在 `search_txt` 函数中,我们打开指定的txt文件,逐行读取该文件并查找是否包含目标字符串。如果发现目标字符串,就调用 `add_result` 函数将该行添加到链表中。最后返回搜索结果链表。
在 `main` 函数中,我们调用 `search_txt` 函数并打印搜索结果。注意,在使用完链表后,需要手动遍历链表并释放每一个节点的内存,防止内存泄漏。
用c语言写一个接口函数,能够搜索txt文件的内容,并使用链表,不适用lunix
以下是一个简单的C语言程序,用于在txt文件中搜索指定字符串,并将结果保存在链表中。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 256
//定义链表节点结构体
typedef struct Node {
char* line;
struct Node* next;
} Node;
//定义搜索函数,返回链表头指针
Node* search(char* filename, char* keyword) {
FILE* fp;
char line[MAX_LINE_LENGTH];
int line_num = 0;
Node* head = NULL;
Node* tail = NULL;
//打开文件
fp = fopen(filename, "r");
if (fp == NULL) {
printf("File not found.\n");
return NULL;
}
//逐行读取文件内容
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
line_num++;
//查找关键字
if (strstr(line, keyword) != NULL) {
//创建新节点
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->line = (char*)malloc(strlen(line) + 1);
strcpy(new_node->line, line);
new_node->next = NULL;
//将新节点加入链表
if (head == NULL) {
head = new_node;
tail = new_node;
} else {
tail->next = new_node;
tail = new_node;
}
}
}
//关闭文件
fclose(fp);
return head;
}
//定义打印函数,用于输出链表内容
void print_list(Node* head) {
Node* p = head;
while (p != NULL) {
printf("%s", p->line);
p = p->next;
}
}
int main() {
char filename[] = "example.txt";
char keyword[] = "hello";
Node* result;
//搜索文件内容
result = search(filename, keyword);
//打印结果
if (result == NULL) {
printf("No match found.\n");
} else {
printf("Match found:\n");
print_list(result);
}
return 0;
}
```
该程序首先定义了一个链表节点结构体,包含了一个指向字符串的指针和一个指向下一个节点的指针。接着定义了一个`search()`函数,该函数接受两个参数:文件名和关键字。该函数逐行读取文件内容,查找包含关键字的行,将结果保存在链表中,并返回链表头指针。最后定义了一个`print_list()`函数,用于输出链表内容。
在`main()`函数中,我们指定了文件名和关键字,调用`search()`函数搜索文件内容,并将结果保存在`result`变量中。最后,根据`result`是否为空,输出相应的提示信息,或者调用`print_list()`函数输出搜索结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)