实现strstr()函数

本文深入解析了C语言中的strstr函数,包括其用法、实现原理及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一)strstr()函数

函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:

#include <stdio.h>
#include <string.h>
int main(void)
{
   char *str1 = "Borland International. Welcome to Wuhan.", *str2 = "nation", *ptr;
   ptr = strstr(str1, str2);
   printf("The substring is: %s\n", ptr);
   return 0;
}
实现函数的源代码:

1.Copyright 1990 Software Development Systems, Inc.

char *strstr( const char *s1, const char *s2 ) 
{ 
	int len2; 

	if ( !(len2 = strlen(s2)) ) 
		return (char *)s1; 

	for ( ; *s1; ++ s1 ) 
	{ 
		if ( *s1 == *s2 && strncmp( s1, s2, len2 ) == 0 ) 
			return (char *)s1; 
  } 
	return NULL; 
} 

2.Copyright 1986 - 1999 IAR Systems. All rights reserved

char *strstr(const char *s1, const char *s2) 
{
	int n;
	if (*s2) 
	{ 
		while (*s1) 
		{ 
			for (n = 0; *(s1 + n) == *(s2 + n); n ++)
			{ 
				if (!*(s2 + n + 1)) 
					return (char *)s1; 
			} 
			s1++; 
		}
		return NULL; 
	} 
	else 
		return (char *)s1; 
}



<think>我们要实现strstr函数,即在一个字符串(haystack)中查找另一个字符串(needle)第一次出现的位置。如果找到,返回第一次出现的起始位置;如果没找到,返回NULL(C语言)或-1(其他语言中可能返回-1,但C标准库是返回NULL)。但是注意,题目要求用C语言实现,因此我们返回指针。标准C库中的strstr函数原型为:char*strstr(constchar*haystack,constchar*needle);实现思路:1.如果needle为空字符串,则直接返回haystack(即位置0)。2.遍历haystack中的每个字符作为起始位置,与needle进行匹配。3.在匹配过程中,如果发现有不匹配的字符,则从haystack的下一个字符重新开始匹配。4.如果匹配成功(即needle的所有字符都匹配),则返回当前起始位置的指针。5.如果遍历完haystack都没有找到,返回NULL。注意:这是一个暴力匹配方法,时间复杂度为O(n*m),其中n是haystack长度,m是needle长度。更高效的算法有KMP、Boyer-Moore等,但题目要求实现strstr,通常面试中暴力解法可以接受,但也可以考虑更高效的算法。这里我们先用暴力法实现,然后可以提一下更高效的算法。暴力法实现:*/</think>### 实现 `strstr` 函数 `strstr` 是 C 标准库中的字符串查找函数,用于在字符串 `haystack` 中查找子串 `needle` 的首次出现位置。以下是使用 C 语言实现的代码: ```c #include <stdio.h> char* my_strstr(const char* haystack, const char* needle) { // 处理空 needle 的情况 if (*needle == '\0') { return (char*)haystack; } // 遍历 haystack for (const char* h = haystack; *h != '\0'; h++) { const char* h_ptr = h; const char* n_ptr = needle; // 尝试匹配子串 while (*h_ptr != '\0' && *n_ptr != '\0' && *h_ptr == *n_ptr) { h_ptr++; n_ptr++; } // 如果完整匹配了 needle if (*n_ptr == '\0') { return (char*)h; } } // 未找到子串 return NULL; } int main() { const char* text = "Hello, this is a test string"; const char* pattern = "test"; char* result = my_strstr(text, pattern); if (result != NULL) { printf("Found at position: %ld\n", result - text); printf("Substring: \"%s\"\n", result); } else { printf("Pattern not found\n"); } return 0; } ``` ### 代码解释 1. **函数原型**: ```c char* my_strstr(const char* haystack, const char* needle); ``` - `haystack`:被搜索的主字符串 - `needle`:要查找的子串 - 返回:指向子串首次出现位置的指针(未找到则返回 NULL) 2. **特殊处理**: ```c if (*needle == '\0') { return (char*)haystack; } ``` - 当 `needle` 是空字符串时,直接返回 `haystack` 起始位置 3. **主循环**: ```c for (const char* h = haystack; *h != '\0'; h++) { // ... } ``` - 遍历 `haystack` 的每个字符作为匹配起点 4. **子串匹配**: ```c while (*h_ptr != '\0' && *n_ptr != '\0' && *h_ptr == *n_ptr) { h_ptr++; n_ptr++; } ``` - 从当前位置尝试匹配 `needle` - 逐个比较字符,直到出现不匹配或到达字符串结尾 5. **匹配成功判断**: ```c if (*n_ptr == '\0') { return (char*)h; } ``` - 如果 `n_ptr` 到达 `needle` 结尾,说明完全匹配 ### 示例输出 ``` Found at position: 16 Substring: "test string" ``` ### 算法分析 - **时间复杂度**:最坏情况 O(m*n),其中 m 是 `haystack` 长度,n 是 `needle` 长度 - **空间复杂度**:O(1),仅使用指针,无额外空间 - **优化方向**:可使用 KMP 算法(O(m+n))或 Boyer-Moore 算法进一步优化效率
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值