版权声明:转载请附上地址 https://blog.csdn.net/weixin_44574520/article/details/87461451
寻找子串位置
题目描述 Description
给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置。
输入描述 Input Description
仅一行包含两个字符串a和b
输出描述 Output Description
仅一行一个整数
样例输入 Sample Input
abcd bc
样例输出 Sample Output
2
数据范围及提示 Data Size & Hint
字符串的长度均不超过100
Pascal用户请注意:两个字符串之间可能包含多个空格
Code:
#include <bits/stdc++.h>
using namespace std;
int main(){
char a[200],b[200];
scanf("%s %s",a,b);
int ax=strlen(a);
int bx=strlen(b);
for(int i=0;i<ax;i++){
if(a[i]==b[0]){
int size=i+1,bj=0;
for(int j=1;j<bx;j++){
if(a[size]!=b[j]){
bj=1;
break;
}
size++;
}
if(!bj){
printf("%d",i+1);
exit(0);
}
}
}
return 0;
}