#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#define SIZE 40
#define LEN 40
char* strncpy1(char*, char*);
char* s_gets(char*, int);
int main(void)
{
char s1[SIZE];
char s2[LEN];
char* st;
int n = 0;
while (n < 5) {
printf("请输入s1字符串:\n");
s_gets(s1, 20);
puts(s1);
printf("请输入s2字符串:\n");
s_gets(s2, 20);
puts(s2);
st = strncpy1(s1, s2);
printf("输出结果为:\n");
printf("%p\n", st);
if (st == NULL)
;
else
puts(st);
n++;
}
return 0;
}
char* strncpy1(char* s1, char*s2)
{
char* st;
int i = 0;
int j = 0;
int t = 0;
while (s1[i]!='\0')
{
while (*s2 != s1[i])
{
if (s1[i] == '\n')
return NULL;
else
i++;
}
st = &s1[i];
t = i;
while (s2[j] == s1[i])
{
i++;
j++;
}
if (s2[j] == '\0')
return st;
else
{
if (s1[i] != '\0')
{
j = 0;
i = t + 1;
}
else
return NULL;
}
}
}
char* s_gets(char* st, int n)
{
char* ret_val;
ret_val = fgets(st, n, stdin);
while( *st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
return ret_val;
}