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


相关推荐

  • HtmlDocument.InvokeScript 方法 (String, Object[])「建议收藏」

    HtmlDocument.InvokeScript 方法 (String, Object[])「建议收藏」HtmlDocument.InvokeScript方法(String,Object[]) 這個方法和.net1.2的execScript方法相似的。execScript在2.0中已經取消了。注意:此方法在.NETFramework2.0版中是新增的。执行在HTML页面中定义的动态脚本函数。命名空间:System.Windows.Forms程序集:

    2022年7月19日
    13
  • 快速制作机房3D效果图教程「建议收藏」

    快速制作机房3D效果图教程「建议收藏」作者:广州麦景科技有限公司林鲁刚 原文接随着信息网络技术的不断发展,大量数据中心的建设,机房监控软件已经成为了机房管理者重要的管理工具,机房监控软件也从无到有,从2D到3D,从静态到三维动态的改进。不多说,直接上图↓以前是这样的现在是这样的或者这样的(麦景数据中心可视化管理平台)现在教大家如何画好一张机房效果图,所用软件有↓前期准备资料

    2022年6月2日
    57
  • jquery 获取或设置radio单选框选中值的方法

    jquery 获取或设置radio单选框选中值的方法jquery获取或设置radio单选框选中值的代码1、获取选中值,三种方法都可以:2、设置第一个Radio为选中值:3、设置最后一个Radio为选中值:4、根据索引值设置任意一个radio为选中值:5、根据Value值设置Radio为选中值6、删除Value值为rd2的Radio7、删除第几个Radio8、遍历Radio1、获取选中值,三种方法都可以:$(‘input:radio:check…

    2022年5月11日
    48
  • 博客备份工具BlogDown 软件使用感想

    博客备份工具BlogDown 软件使用感想最近在找博客备份相关的工具,看到了一个不错的博客备份工具BlogDown。使用博客备份BlogDown工具是可以制作博客电子书的。他支持导出多种文件格式,包括常用的电子书格式chm,还有word格式doc而且他导出的文件中包含博客中的图片,无用联网,是真正的博客图片备份,不是只备份图片地址。对于我们这些博客爱好者来说是很好的工具,对于文章的收藏也很方便。在博客爱好者中,刚开

    2022年7月25日
    10
  • 【PotPlayer】敲好用的本地视频播放器

    【PotPlayer】敲好用的本地视频播放器软件简介PotPlayer是KMPlayer的原作者姜勇囍进入Daum公司后的新一代作品,目前仍有更新。由于采用Delphi编译程序的KMPlayer有一些弊端,姜勇囍为改进播放器本身的一些性能而重新用VC++进行构架。虽然PotPlayer与KMPlayer同属一个开发者的产品,但它与KMPlayer所注重的地方并不同,能够满足不同用户的使用需求。因该软件的官方网站托管于DAUM平台,中国大陆网络可能受防火长城(GFW)影响而无法正常访问。官方网站https://potplayer.daum.

    2022年7月14日
    37
  • NeatUpload的安装使用

    NeatUpload的安装使用版本:NeatUpload-1.2.32,用于文件上传。可传大文件。1.在VS工具箱中点右键选“选择项”……将Brettle.Web.NeatUpload.dll添加到工具箱。可以在添加后的工具箱看到

    2022年7月2日
    27

发表回复

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

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