//输入字符串A和B,将A中和B字符串相同的子串删除,并输出
#include <stdio.h>
#include <string.h>
void delete(char *str,char *ptr,char *result) //删除子串
{
char *p1 = str; //字符串A
char *p2 = ptr; //字符串B
// int count = 0;
// int flag = 0;
int n1 = strlen(str);
int n2 = strlen(ptr);
if(str == NULL || ptr == NULL)
{
return -1;
}
while(*p1 != '\0')
{
if(*p2 == '\0')
{
*result -= n2;
p2 = ptr;
continue;
}
*result = *p1;
if(*p1 == *p2)
{
p2++;
p1++;
}
*result = '\0';
}
/*
while(*p1 != '\0')
{
count = 0;
p2 = ptr;
while(*p1 == *p2)
{
p1++;
p2++;
count++;
}
if(*p2 != '\0')
{
p1 = p1 -count;
}
else
{
flag = 0;
}
*result = *p1;
result++;
p1++;
}
*result = '\0';
}
*/
int main()
{
char str[100] = {0};
char ptr[100] = {0};
char result[100] = {0};
printf("Please input string A:");
scanf("%s",str);
printf("Please input string B:");
scanf("%s",ptr);
delete(str,ptr,result);
printf("%s",result);
/* if(ret == -1)
{
printf("error!");
}
else
{
printf("%s",result);
}*/
return 0;
}
删除子串
最新推荐文章于 2023-01-03 16:36:31 发布