This question requires a simple function of the search string.
Function interface definition:
char * Search (S char *, char * T);
Substring search function to find the string s t, the return address of the first sub-string t in s. If not found, NULL is returned.
Sample testing procedures referees:
#include <stdio.h>
#define the MAXS 30
Search * char (S char *, char T);
void the ReadString (S char []); / ref provided, details are not list * /
int main ()
{
char s [Maxs], t [Maxs], * post;
ReadString(s);
ReadString(t);
pos = search(s, t);
if ( pos != NULL )
printf("%d\n", pos - s);
else
printf("-1\n");
return 0;
}
/ * Your code will be embedded here * /
Sample Input 1:
at The C Programming Language
RAM
Output Example 1:
10
Sample Input 2:
at The C Programming Language
here. Bored
Output Sample 2:
-1
char *search( char *s, char *t )
{
int i, j, k;
char *p = NULL;
for (i = 0, j = 0; s[i] != '\0'; i++)
{
k = i; j = 0;
// 不可以直接用i
//初始化j, 保证t[j]每一次都是由第一个字母开始
while (t[j] == s[k])
{
j++; k++;
if (t[j] == '\0')
{
p = &s[i]; //也可以直接return (s + i)
return p;
}
}
}
return p;
}