实现strstr()函数
2012-02-09 19:31
429 查看
一)strstr()函数
函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:
1.Copyright 1990 Software Development Systems, Inc.
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;
}
函数名: 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;
}
相关文章推荐
- 79 实现数组排序 实现 strstr()函数功能
- 模拟实现字符串操作函数(strcpy,strstr,strcat,strcmp,strlen)与内存操作函数(memcpy,memmove)
- strstr()函数c++代码实现实例及运行结果
- 字符串操作之strstr()函数实现
- Linux中strchr与strstr函数实现。
- strstr和memcmp函数的实现
- 系统函数C字符串的实现(2):strstr
- 模拟实现strcmp,strncmp,strstr,strcat,strncat,strcpy,strncpy,strlen ,memcpy,memmove等函数
- 模拟实现C库函数strcpy,strcat,strstr,strchr,strcmp,memcpy,memmove
- c语言实现 strstr 函数
- strstr()函数c++代码实现实例及运行结果
- 字符串操作函数 strstr 实现
- C 函数 strstr 的高效实现
- [LeetCode]44. Implement strStr()实现strStr()函数
- 浅谈 my_strstr()函数与my_strrstr()函数的实现
- 自定义实现字符串处理函数 strlen strcmp strcpy strncpy strcat strncat strchr strstr
- strstr()函数c++代码实现实例及运行结果
- strstr()函数的实现
- strstr()函数的具体实现
- C 标准库 strstr 函数的实现