strstr

编辑

strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。

C语言函数

编辑

包含文件:string.h
函数名: strstr
函数原型:
1
extern char *strstr(char *str1, const char *str2);
语法:
1
strstr(str1,str2)
str1: 被查找目标 string expression to search.
str2: 要查找对象 The string expression to find.
返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。
例子:
1
2
3
char str[]="1234xyz";
char *str1=strstr(str,"34");
cout << str1 << endl;
显示的是: 34xyz

函数实现

1.Copyright 1990 Software Development Systems, Inc.
1
2
3
4
5
6
7
8
9
10
11
12
char *strstr(const char *s1,const char *s2)
{
 int len2;
 if(!(len2=strlen(s2)))//此种情况下s2不能指向空,否则strlen无法测出长度,这条语句错误
     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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char *strstr(constchar*s1,constchar*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;
}
3. GCC-4.8.0
1
2
3
4
5
6
7
8
9
10
11
char *strstr(const char*s1,const char*s2)
{
    const char*p=s1;
    const size_tlen=strlen(s2);
    for(;(p=strchr(p,*s2))!=0;p++)
    {
        if(strncmp(p,s2,len)==0)
            return (char*)p;
    }
    return(0);
}
  

应用举例

// strstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <syslib.h>
#include <string.h>
main()
{
    char *s="GoldenGlobalView";
    char *l="lob";
    char *p;
    clrscr();
    p=strstr(s,l);
    if(p)
        printf("%s",p);
    else
        printf("NotFound!");
    getchar();
    return0;
}
//功能:从字串” string1 onexxx string2 oneyyy”中寻找”yyy”
(假设xxx和yyy都是一个未知的字串)
1
2
3
4
5
6
7
char *s=”string1onexxxstring2oneyyy”;
char *p;
p=strstr(s,”yyy”);
if(p!=NULL)
    printf(“%s”,p);
else
    printf("notfound\n");
说明:如果直接写语句p=strstr(s,”one”),找到的是onexxxstring2oneyyy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char *mystrstr(char*s1,char*s2)
{
    if(*s1==0)
    {
        if(*s2)
            return (char*)NULL;
        return (char*)s1;
    }
    while(*s1)
    {
        int i=0;
        while(1)
        {
            if(s2[i]==0)
                return s1;
            if(s2[i]!=s1[i])
                break;
            i++;
        }
        s1++;
    }
    return (char*)NULL;
}

strstr 函数用法的更多相关文章

  1. C语言——常用标准输入输出函数 scanf(), printf(), gets(), puts(), getchar(), putchar(); 字符串拷贝函数 strcpy(), strncpy(), strchr(), strstr()函数用法特点

    1 首先介绍几个常用到的转义符 (1)     换行符“\n”, ASCII值为10: (2)     回车符“\r”, ASCII值为13: (3)     水平制表符“\t”, ASCII值为 9 ...

  2. php中strstr、strrchr、substr、stristr四个函数用法区别

    php中strstr.strrchr.substr.stristr四个函数用法区别: php中strstr strrchr substr stristr这四个字符串操作函数特别让人容易混淆,常用的是s ...

  3. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  4. string.h文件中函数用法

    下面为string.h文件中函数的详细用法: strcpy函数名:strcpy功 能: 拷贝一个字符串到另一个用 法: char *strcpy(char *destin, char *source) ...

  5. PHP strstr() 函数

    实例 查找 "world" 在 "Hello world!" 中是否存在,如果是,返回该字符串及后面剩余部分: <?php echo strstr(&qu ...

  6. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  7. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  8. memcpy函数用法

    memcpy函数用法 .分类: VC++ VC++ mfc matlab 2011-12-01 19:17 14538人阅读 评论(0) 收藏 举报 null 原型:extern void *memc ...

  9. Python回调函数用法实例详解

    本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...

随机推荐

  1. HDU5802-windows 10-dfs+贪心

    音量减的时候,分两种,一种是减到大于目标M,另一种是减到小于M,停顿的时候可以减少最后往上加的次数,小于0的时候变成0 然后比一下这两种的最小值. /*------------------------ ...

  2. 关于UITextView / String的尺寸

    关于UITextView以及String的尺寸动态获取 iOS7开始,UITextView设置text后不会立即反映到contentSize属性,而是在父容器layoutSubviews时进行cont ...

  3. Android中的Intent Filter匹配规则介绍

    本文主要介绍了隐式Intent匹配目标组件的规则,若有叙述不清晰或是不准确的地方希望大家指出,谢谢大家: ) 1. Intent简介 Intent用于在一个组件(Component,如Activity ...

  4. C# Winform实现炫酷的透明动画界面

    做过.NET Winform窗体美化的人应该都很熟悉UpdateLayeredWindow吧,UpdateLayeredWindow可以实现窗体的任意透明,效果很好,不会有毛边.不过使用这个API之后 ...

  5. El Capitan 中 SIP 介绍

    http://havee.me/mac/2015-10/system-integrity-protection-on-el-capitan.html 这两天大家纷纷将 OS X 系统升级到了 El C ...

  6. Git.Framework 框架随手记--ORM项目工程

    前面已经简单介绍过了该框架(不一定是框架),本文开始重点记录其使用过程.可能记录的内容不是太详尽,框架也可能非常烂,但是里面的代码句句是实战项目所得.本文非教唆之类的文章,也非批判之类的文章,更不是炫 ...

  7. [poj2184]我是来水一下背包的

    http://poj.org/problem?id=2184 题意:01背包的变种,就是说有2组值(有负的),你要取一些物品是2阻值的和非负且最大 分析: 1.对于负的很好处理,可以把他们都加上一个数 ...

  8. 温故知新---重读C#InDepth(一)

    一本好书,或是一本比较有深度的书,就是每次研读的时候都会有新的发现. 好吧,我承认每次读的时候都有泛泛而过的嫌疑~~ 这几年一直专注于C#客户端的开发,逐步从迷迷糊糊,到一知半解,再到自以为是,最后沉 ...

  9. Java版本-----商店购物系统

    buy.java public class Buy { public static void main(String[] args) { // TODO Auto-generated method s ...

  10. [团队项目]Scrum 项目1.0 (演说视频)NABCD

    1.确定选题. 应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 录制为演说视频,上传到视频网站,并把链接发到团队博客上. 截止日期:2016.5.6日晚10点 2.SCRUM 流 ...