Description
给定两个字符串string1和string2,判断string2是否为string1的子串。Input
输入包含多组数据,每组测试数据包含两行,第一行代表string1,第二行代表string2,string1和string2中保证不出现空格。Output
对于每组输入数据,若string2是string1的子串,则输出"YES",否则输出"NO"。Sample Input
abc a 123456 45 abc dddSample Output
YES YES NO
#include <stdio.h> #include <stdlib.h> #include"string.h" int main() { int i,j,m,n; char s1[1000]={"\0"},s2[1000]={"\0"},*p=s2; while (scanf("%s%s",s1,s2)!=EOF) { m=strlen(s1); n=strlen(s2); if (n>m) printf("NO\n"); else { for(j=0;j<=(m-n);j++) { if(*p==s1[j]) { for(i=0;i<n;i++) if(*(p+i)==s1[j+i]) continue; else break; } else continue; if(i==n) {printf("YES\n"); break; } } if(j>(m-n)) printf("NO\n"); } } return 0; }