首页 > 其他 > 详细

[codevs 1204]寻找子串位置

时间:2018-01-18 00:28:24      阅读:240      评论:0      收藏:0      [点我收藏+]

注意引入的新函数strstr

C语言函数

包含文件:string.h

函数名: strstr

函数原型:

语法:

str1: 被查找目标 string expression to search.

str2: 要查找对象 The string expression to find.

返回值:若str2是str1的子串,则先确定str2在str1的第一次出现的位置,并返回此str1在str2首位置的地址。;如果str2不是str1的子串,则返回NULL。

例子:

显示的是: 34xyz

折叠函数实现

1.Copyright 1990 Software Development Systems, Inc.

2.Copyright 1986 - 1999 IAR Systems. All rights reserved

3. GCC-4.8.0

 

折叠应用举例

// strstr.c

//功能:从字串" string1 onexxx string2 oneyyy"中寻找"yyy"

(假设xxx和yyy都是一个未知的字串)

说明:如果直接写语句p=strstr(s,"one"),找到的是onexxxstring2oneyyy(来源:360百科)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int main()  
 6 {  
 7     char a[100],b[100];  
 8     cin>>a>>b;
 9     if (strlen(a)>strlen(b))//注意和a>b进行区分  
10     {  
11         cout<<strstr(a,b)-a+1;//找到a中第一次出现b的指针  
12     }  
13     return 0;  
14 }  

[codevs 1204]寻找子串位置

原文:https://www.cnblogs.com/wwcjj/p/8306660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!