code[vs] 1204 寻找子串位置

来源:互联网 发布:战争电影推荐 知乎 编辑:程序博客网 时间:2024/05/29 15:14


#include<iostream>#include<cstring>using namespace std;const int maxn = 110;char a[maxn] , b[maxn];int main(){cin >> a >> b;//直接用strstr获得小串在大串里的指针//cout << (strstr(a,b) - a)/sizeof(char)+1;//以大串的每一个字符开始去在小串一个一个的找int al = strlen(a) , bl=strlen(b);for(int i = 0;i<al;i++){int ok = true;for(int j = 0;j<bl;j++){if(a[i+j] != b[j]){ok= false;break;}}if(ok){cout << i+1;break;}}return 0;}