C语言之strstr函数

C语言之strstr函数【FROMMSDN&&百科】原型:char*strstr(constchar*str1,constchar*str2);#include找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。Returnsapointertothefirstoccurrence

大家好,又见面了,我是你们的朋友全栈君。

【FROM MSDN && 百科】

原型:char *strstr(const char *str1, const char *str2);

#include<string.h>

找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。返回该位置的指针,如找不到,返回空指针。

Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear in str. If strSearch points to a string of zero length, the function returns str.

DEMO: mystrstr



#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#pragma warning (disable:4996)
char *mystrstr(char *s1,char *s2);
int main(void)
{
	char *s="Golden Global View";
	char *l="ob";   //char *l=""
	char *p;
	system("cls");
	p=mystrstr(s,l);
	if (p!=NULL)
	{
		printf("%s\n",p);
	}
	else
	{
		printf("Not Found!\n");
	}
    getch();
	return 0;
}
/*FROM 百科*/
char *mystrstr(char *s1,char *s2)
{
	int n;
	if (*s2)                      //两种情况考虑
	{
        while(*s1)               
		{
            for (n=0;*(s1+n)==*(s2+n);n++)
            {
				if (!*(s2+n+1))            //查找的下一个字符是否为'\0'
				{
					return (char*)s1;
				}
            }
			s1++;
		}
		return NULL;
	}
	else
	{
		return (char*)s1;
	}
}

DEMO:

//#define FIRST_DEMO
#define SECOND_DEMO

#ifdef FIRST_DEMO
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(void)
{
	char *s="Golden Global View";
	char *l="lob";
	char *p;
	system("cls");
	p=strstr(s,l);
	if (p!=NULL)
	{
		printf("%s\n",p);
	}
	else
	{
		printf("Not Found!\n");
	}

	getch();
	return 0;
}
#elif defined SECOND_DEMO
/*从字串” string1 onexxx string2 oneyyy”中寻找”yyy”*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(void)
{
	char *s="string1 onexxx string2 oneyyy";
	char *p;
	p=strstr(s,"string2");
	printf("%s\n",p);
	if (p==NULL)
	{
		printf("Not Found!\n");
	}
	p=strstr(p,"one");
	printf("%s\n",p);
	if (p==NULL)
	{
		printf("Not Found!\n");
	}
	p+=strlen("one");
	printf("%s\n",p);

	getch();
	return 0;
}
#endif

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153025.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 单片机毕业设计196例「建议收藏」

    单片机毕业设计196例「建议收藏」单片机本科毕业设计——心率计(脉搏测量仪)系统设计与实现(源代码+protues仿真+PCB+开题报告+讲解视频).zip,相关下载链接:https://download.csdn.net/download/dwf1354046363/72630770单片机本科毕业设计——声控灯(继电器)控制系统设计与实现(源代码+protues仿真+PCB+开题报告+讲解视频).zip,相关下载链接:https://download.csdn.net/download/dwf1354046363/72620013单片

    2022年10月4日
    5
  • MySQL数据库:事务和ACID实现原理

    MySQL数据库:事务和ACID实现原理

    2021年4月10日
    191
  • python中callback_python安装后怎么打开

    python中callback_python安装后怎么打开刚接触Python的时候,简单的异常处理已经可以帮助我们解决大多数问题,但是随着逐渐地深入,我们会发现有很多情况下简单的异常处理已经无法解决问题了,如下代码,单纯的打印异常所能提供的信息会非常有限。deffunc1():raiseException(“–func1exception–“)defmain():try:func1()exceptExceptionase:printe…

    2025年5月28日
    3
  • Buildroot 用户手册 (中文)

    Buildroot 用户手册 (中文)文章目录I.Gettingstarted1.AboutBuildroot2.Systemrequirements2.1.Mandatorypackages2.2.Optionalpackages3.GettingBuildroot4.Buildrootquickstart4.1configuration4.2build5.CommunityresourcesII.Userguide6.Buildrootconfiguration6.1.Cross-compil

    2022年10月20日
    2
  • 性能监控平台搭建 — 集成Locust性能数据

    性能监控平台搭建 — 集成Locust性能数据无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。文章目录问题概述获取Locust性能数据接口定时采集性能数据性能数据采集一致性no-web模式下获取性能数据slave模式下不进行数据采集封装原文链接之前的几篇关于性能监控平台搭建的文章,分别介绍了性能测试中的资源数据采集…

    2022年5月20日
    39
  • LeetCode琅琊榜第二层-最长回文子串问题(动态规划)「建议收藏」

    LeetCode琅琊榜第二层-最长回文子串问题(动态规划)「建议收藏」LeetCode_5.最长回文字串难度:中等看了它,妈妈再也不用担心我不会力扣啦

    2022年10月16日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号