char *search(char *s, char t);
void ReadString( char s[] ); / 裁判提供,细节不表 */
int main()
{
char s[MAXS], t[MAXS], *pos;
ReadString(s);
ReadString(t);
pos = search(s, t);
if ( pos != NULL )
printf("%d\n", pos - s);
else
printf("-1\n");
return 0;
}
/* 你的代码将被嵌在这里 */
char *search( char *s, char t )
{
int flag=1;
char b;
while(s!='\0')
{
if(s==t)
{
b=t;
for(;(t++)!='\0';)
{
if((s++)!=(t))
flag=0;
}
if(flag==1)
{
return b;
}
}
s++;
}
return NULL;
}
供参考:
#include <stdio.h>
#define MAXS 30
char* search(char* s, char* t);
void ReadString( char s[] ); /*裁判提供,细节不表*/
int main()
{
char s[MAXS], t[MAXS], * pos;
ReadString(s);
ReadString(t);
pos = search(s, t);
if (pos != NULL)
printf("%d", pos - s);
else
printf("Not Found");
return 0;
}
/* 你的代码将被嵌在这里 */
char* search(char* s, char* t)
{
int i, j, k, flg;
char* pos = NULL;
for (i = 0, flg = 1; flg && s[i] != '\0'; i++)
for (j = i, k = 0; t[k] == s[j]; k++, j++)
if (t[k + 1] == '\0') { pos = s + i; flg = 0; break; }
return pos;
}