验证子串

来源:互联网 发布:冰锐和锐澳的区别 知乎 编辑:程序博客网 时间:2024/05/22 05:01

验证子串


时间限制: 1000 ms         内存限制: 65536 KB
提交数: 382     通过数: 196 

【题目描述】

输入两个字符串,验证其中一个串是否为另一个串的子串。

【输入】

输入两个字符串, 每个字符串占一行,长度不超过200且不含空格。

【输出】

若第一个串s1是第二个串s2的子串,则输出(s1) is substring of (s2)

否则,若第二个串s2是第一个串s1的子串,输出(s2) is substring of (s1)

否则,输出 No substring。

【输入样例】

abcdddncabca

【输出样例】

abc is substring of dddncabca

【来源】


No

【代码】

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<iomanip>using namespace std;int main(){char s1[200],s2[200];//按照题目要求定义字符数组char tem1[200],tem2[200];int i,j;gets(s1);gets(s2);if(strlen(s1)<=strlen(s2))//保证tem2数组比tem1数组要长{strcpy(tem1,s1);strcpy(tem2,s2);}else {strcpy(tem1,s2);strcpy(tem2,s1);}for(i=0;i<strlen(tem2);i++)//同时遍历tem2数组和tem1数组{if(tem1[0]==tem2[i]){for(j=0;j<strlen(tem1);j++){if(tem1[j]==tem2[i+j]) {if(j==strlen(tem1)-1) {printf("%s is substring of %s",tem1,tem2);return 0;}}else break;}}}cout<<"No substring";return 0;}

【说明】

1.vc6.0运行成功,提交通过。


原创粉丝点击