从键盘输入父串,子串,要求删除父串中的子串。

来源:互联网 发布:芒果tv php视频解析 编辑:程序博客网 时间:2024/06/04 01:10

如:
父串:“hellosdhellodsfhellohello”
子串:“hello”
要求删除父串中的hello。

#include <stdio.h>#include <string.h>#define MAX_SIZE 1024int 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;}char *delete_aim_str(char *src, char *delete){    int i;    int d_len = strlen(delete);    int s_len = strlen(src);    char *dest = src;    while(*src != '\0')    {        if(my_strncmp(src,delete,d_len) == 0)        {            for(i = 0; i < s_len - d_len; i++)            {                *(src+i) = *(src + d_len+i);            }        }        src++;    }    return dest;}int main(){    char src[MAX_SIZE];    char delete[MAX_SIZE];    printf("Please input string src:\n");    scanf("%s",src);    printf("Please input string to be deleted:\n");    scanf("%s",delete);    char *result = delete_aim_str(src,delete);    printf("The result is:\n%s\n",result);    return 0;}
0 0
原创粉丝点击