代码语言:javascript复制
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int test(char* s1, char* s2, int length1, int length2)
{
int temp = 0;
int count = 0;
for (int i = 0; i < length1; i )
{
count = 0;
if (s1[i] == s2[0])
{
temp = i;
for (int j = i, k = 0; j < length1, k < length2; j , k )
{
if (s1[j] == s2[k])
{
count ;
}
else
{
break;
}
}
if (length2 == count)
{
return temp;
}
}
}
return -1;
}
int main(void)
{
char s1[20];
char s2[20];
cin >> s1;
cin >> s2;
int ret = test(s1, s2, strlen(s1), strlen(s2));
if (ret < 0)
{
cout << "没找到" << endl;
}
else
{
cout << "s2的第一个字符在s1的下标是" << ret;
}
return 0;
}