69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
#define MAXLINE 255
void del_sub(char *str, const char *sub);
int main()
{
char str[MAXLINE];
char sub[MAXLINE];
while(gets(str) && gets(sub))
{
del_sub(str, sub);
}
return 0;
}
void del_sub(char *str, const char *sub)
{
char *p = str;
const char *q = sub;
char arr[MAXLINE];
char *m = arr;
char *temp;
while(*p)
{
if(*p == *q)
{
temp = p; //temp记录字符串和子串匹配的起始位置
while(*q && *p == *q)
{
p++;
q++;
}
if(*q == '\0') //如果子串遍历结束,重置指针q到子串
q = sub;
else if(*p == '\0') //如果失配时字符串遍历完,说明
{ //从temp记录处到字符串结尾没有匹配
while(*temp) //子串的情况,如aaaaaaa,aaaa这种情况
*m++ = *temp++;
}
else //字符串中部分匹配子串时,回溯指针p
*m++ = *temp, p = temp + 1;
/*else
{
ttemp = temp;
while(temp < p)
*m++ = *temp++;
p = ++ttemp;
}*/
}
else
*m++ = *p++;
q = sub;
}
*m = '\0';
//strcpy(str, arr);
puts(arr);
}
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
char src[100],sub[100];
gets(src);
gets(sub);
int len1=strlen(src);
int len2=strlen(sub);
int sta=0,tail=0;
int j=0;
for(int i=0;i<len1;i++)
{
if(j>=len2)
break;
if(src[i]!=sub[j])
{
j=0;
sta=0;
tail=0;
continue;
}
else if(sta==tail)
{
sta=i;
tail=i+1;
j++;
}
else
{
tail=i+1;
j++;
}
}
if(tail==sta)
puts(src);
else
{
for(int i=tail;i<len1;i++)
{
src[sta++]=src[i];
}
src[sta]=0;
puts(src);
}
return 0;
}