[codevs 1204]寻找子串位置

注意引入的新函数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 }  
posted @ 2018-01-17 22:55  Do1phln  阅读(517)  评论(0编辑  收藏  举报