定義三個字符串:str[],ab[],output[],str是第壹個輸入字符串,output是最終結果。
設置兩個下標I,j. I指向第壹個字符串,j指向第二個子字符串;
I每次向後移動壹個位置,I的下壹個strlen(ab)字符是否與字符串ab匹配,如果不匹配,將str[i]加到字符串輸出中,如果匹配,讓I直接跳過strlen(ab)位置;
詳見代碼!自己在紙上畫個圖就行了。
# include & ltstdio.h & gt
# include & ltstring.h & gt
int main(void)
{
char str[30],ab[5],output[30];
scanf("%s ",str);
scanf("%s ",ab);
int i,j,output index = 0;
for(I = 0;我& ltstrlen(str);i ++)
{
for(j = 0;j & ltstrlen(ab)& amp;& ampI+j & lt;strlen(str);j ++)
{
if(str[i + j]!= ab[j])break;
}
如果(j!= strlen(ab))
output[output index ++]= str[I];
其他
I = I+strlen(ab)-1;
}
output[output index]= ' \ 0 ';
printf("%s\n ",輸出);
返回0;
}