main主函数
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 1024
int main()
{
char src[MAX_SIZE];
char head[MAX_SIZE];
char tail[MAX_SIZE];
printf("Please input src string:\n");
scanf("%s",src);
printf("Please input head string:\n");
scanf("%s",head);
printf("Please input tail string:\n");
scanf("%s",tail);
char *result = find_aim_str(head,tail,src);
printf("result string = %s\n",result);
return 0;
}
找子串
int my_strncmp(char *s1, char *s2, int len)
{
int i;
for(i = 0; i < len; i++)
{
if(*(s1 + i) != *(s2 + i))
{
return -1;
}
}
return 0;
}
输出head和tail
#include <stdio.h>
char * find_aim_str(char *head, char *tail, char *src)
{
char *temp;
int h_len = strlen(head);
int t_len = strlen(tail);
while(*src != '\0')
{
if(my_strncmp(src,head,h_len) == 0)
{
temp = src;
src = src + h_len;
while(*src != '\0')
{
if(my_strncmp(src,tail,t_len) == 0)
{
*(src + t_len) = '\0';
return temp;
}
src++;
}
return NULL;
}
src++;
}
return NULL;
}