串结构练习——字符串匹配


串结构练习——字符串匹配

 

Time Limit: 1000MS Memory limit: 65536K

题目描述

  给定两个字符串string1和string2,判断string2是否为string1的子串。
 

输入

 输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2,string1和string2中保证不出现空格。
 

输出

 对于每组输入数据,若string2是string1的子串,则输出"YES",否则输出"NO"。
 

示例输入

abc
a
123456
45
abc
ddd

示例输出

YES
YES
NO

提示

 

来源

 赵利强

示例程序

 
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main()
{
    char st1[101],st2[101];
    while(~scanf("%s%s",st1,st2))
    {
        if(strstr(st1,st2))
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}

注释:函数中运用的strstr()函数,包含在string.h中;strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。该函数是二进制安全的。

posted @ 2014-11-19 23:00  夏迩  阅读(118)  评论(0编辑  收藏  举报