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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java基础案例4-4学生和老师「建议收藏」

    java基础案例4-4学生和老师「建议收藏」packagecom.itheima;importjava.util.Scanner;abstractclassPerson{voidspeak(){}}classTeacherextendsPerson{privateStringname;voidsetName(Stringname){this.name=name;}StringgetName(){returnname;.

    2022年6月20日
    35
  • navitecat激活码[最新免费获取]2022.03.11

    (navitecat激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1M…

    2022年3月13日
    154
  • Bulk Insert命令具体

    Bulk Insert命令具体

    2021年9月3日
    59
  • MMC卡驱动分析

    MMC卡驱动分析MMC卡驱动分析 最近花时间研究了一下MMC卡驱动程序,开始在网上找了很多关于MMC卡驱动的分析文章,但大都是在描述各个层,这对于初学者来讲帮助并不大,所以我就打算把自己的理解写下来,希望对大家有用。个人觉得理解LINUX内核当中MMC/SD卡驱动程序构架是学习MMC卡驱动程序的重点,只有理解了它的基本框架或流程才能真正理解一个块设备驱动程序的写法,同时才能真正理

    2022年4月28日
    58
  • 查看Linux版本

    查看Linux版本

    2021年10月18日
    44
  • onedrive自动同步_onedrive没有同步

    onedrive自动同步_onedrive没有同步Zotero管理文献时,经常需要同步不同设备的文献到Zotero云端以保持版本统一,但是Zotero提供的免费空间不够用来同步大量pdf附件。这时,可以使用第三方云平台来实现同步,比如坚果云,onedrive等。这里,我用onedrive来存储Zotero的pdf文件。………

    2022年9月8日
    1

发表回复

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

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