strstr



#include <stdio.h>
02  #include <windows.h>
03  
04  int mystrstr(char *addr1, char *addr2)
05  {
06      int m,n;
07      int x;
08      x=strlen(addr1);
09      for (n=0; n<=strlen(addr1); n++)
10      {
11          if (*addr1+n == *addr2)
12          {            
13              for (m=0; m<=strlen(addr2); m++)
14              {
15                  if (*addr1+n+m != *addr2+m)
16                  {
17                      break;
18                  }
19                  if (m == strlen(addr2))
20                  {
21                      return n;
22                  }
23              }            
24          }    
25  
26      }
27  
28      return 0;
29  }
30      int    main()
31        {
32          int num;
33          num = mystrstr("12345678","78");
34          printf("%d",num);
35          return 0;
36        }

 

 

现在用汇编写一下:

01  strstr proc pszHaystack:PTR BYTE, pszNeedle:PTR BYTE, dwCaseSensitive:DWORD
02      push    esi
03      push    edi
04      xor    eax, eax
05      xor    ecx, ecx
06      xor    edx, edx
07      mov    esi, [pszHaystack]
08      mov    edi, [pszNeedle]
09  @@char:    mov    al, byte ptr [esi]
10      mov    ah, byte ptr [edi]
11      cmp    [dwCaseSensitive], 0
12      jne    @@cmp1
13      cmp    al, "Z"
14      ja    @@cmp0
15      cmp    al, "A"
16      jb    @@cmp0
17      add    al, 32
18  @@cmp0:    cmp    [dwCaseSensitive], 0
19      jne    @@cmp1
20      cmp    ah, "Z"
21      ja    @@cmp1
22      cmp    ah, "A"
23      jb    @@cmp1
24      add    ah, 32
25  @@cmp1:    cmp    al, ah
26      jne    @@next
27      inc    ecx
28      mov    eax, esi
29      inc    edi
30      inc    esi
31      inc    eax
32      cmp    byte ptr [esi], 0
33      je    @@zero
34      cmp    byte ptr [edi], 0
35      je    @@quit
36      jmp    @@char
37  @@zero:    mov    dl, byte ptr [edi]
38      cmp    dl, byte ptr [esi]
39      je    @@quit
40      xor    eax, eax
41      xor    ecx, ecx
42      jmp    @@quit
43  @@next:    mov    edi, [pszNeedle]
44      test    ecx, ecx
45      setz    dl
46      add    esi, edx
47      xor    ecx, ecx
48      xor    eax, eax
49      cmp    byte ptr [esi], 0
50      jne    @@char
51  @@quit:    sub    eax, ecx
52      pop    edi
53      pop    esi
54      ret
55  strstr endp

10-20
<think>我们正在讨论`strstr`函数的使用方法。根据引用内容,`strstr`函数用于在字符串中查找子串,并返回第一次出现的位置的指针。如果未找到,则返回NULL。 关键点: 1. 函数原型:`char *strstr(const char *haystack, const char *needle);` 2. 参数说明: - haystack: 要被查找的字符串(主串) - needle: 要查找的子串 3. 返回值: - 找到:返回指向主串中第一次出现子串的位置的指针(即子串第一个字符在主串中的地址) - 未找到:返回NULL 我们将通过步骤详解来介绍使用方法,并给出示例代码。 </think>### `strstr`字符串查找函数详解 #### 一、核心功能与语法 `strstr()` 用于在主字符串中查找子字符串,函数原型为: ```c char *strstr(const char *haystack, const char *needle); ``` - **参数说明** - `haystack`:待搜索的主字符串(如 `"Hello world!"`) - `needle`:要查找的子字符串(如 `"world"`) - **返回值** - 找到子串:返回**首次出现位置的指针**(如指向主串中 `'w'` 的地址) - 未找到:返回 `NULL` ```c #include <stdio.h> #include <string.h> // 必须包含的头文件 int main() { char str1[] = "This is a simple string"; char *result = strstr(str1, "simple"); // 查找子串 printf("找到的位置: %s\n", result); // 输出: "simple string" } ``` [^1] --- #### 二、使用步骤与示例 ##### 1. 基础查找 ```c char email[] = "user@example.com"; char *domain = strstr(email, "@"); if (domain != NULL) { printf("域名: %s\n", domain + 1); // 输出: "example.com" } ``` ##### 2. 替换子串(结合 `strncpy`) ```c char text[] = "I like apples"; char *pos = strstr(text, "apples"); if (pos) { strncpy(pos, "oranges", 7); // 替换为等长的"oranges" printf("%s\n", text); // 输出: "I like oranges" } ``` [^4] ##### 3. 检测子串存在性 ```c char log[] = "Error: File not found"; if (strstr(log, "Error") != NULL) { printf("系统发生错误\n"); } ``` --- #### 三、模拟实现(BF算法) ```c char* my_strstr(const char* str1, const char* str2) { if (*str2 == '\0') return (char*)str1; // 空子串直接返回主串 const char *s1 = str1; while (*s1) { const char *p = s1; // 主串当前位置 const char *s2 = str2; // 子串起始位置 // 逐字符比较 while (*p && *s2 && *p == *s2) { p++; s2++; } if (*s2 == '\0') return (char*)s1; // 子串全部匹配 s1++; // 主串指针后移 } return NULL; // 未找到 } ``` [^5] --- #### 四、关键注意事项 1. **区分大小写**: `strstr("ABC", "abc")` 返回 `NULL`(大小写敏感) 2. **空子串处理**: 若 `needle` 为空字符串(`""`),返回 `haystack` 首地址 3. **重叠内存安全**: `strstr` 本身安全,但结合 `strncpy` 修改时需确保目标空间足够 4. **性能特性**: - 时间复杂度:最坏情况 $O(n \times m)$($n,m$ 为主串和子串长度) - 适合短文本搜索,长文本建议用 KMP 等高效算法 --- #### 五、典型错误案例 ```c char msg[20] = "Hello"; char *p = strstr(msg, "Hello"); strncpy(p, "Hi", 3); // 错误!"Hi"仅2字符,但复制3字节会覆盖后续内容 // 正确写法:strncpy(p, "Hi\0", 3); 或确保目标空间独立 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值