pointers on c 编程练习 6-2
程序员文章站
2022-03-11 18:17:16
...
编写一个函数,用于删除字符串中的固定字符
<textarea readonly="readonly" name="code" class="c">
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int del_substr(char *str,char const *substr);
void main()
{
char st[100],sb[100];
gets(st);
gets(sb);
printf("%d\n",del_substr(st,sb));
puts(st);
}
int del_substr(char *str,char const *substr)
{
int flag;
char *a,*b;
flag = FALSE;
b = substr;
while(*str != '\0')
{
if(*str == *substr)
{
a = str;
while(*str++ == *substr++ && *(substr+1)!='\0')
;
if(*substr == '\0')
{
flag = TRUE;
break;
}
substr = b;
}
str+=1;
flag = FALSE;
}
if(!flag)
return FALSE;
else
{
while(*str != '\0')
{
*a++ = *str;
str+=1;
}
*a = '\0';
return TRUE;
}
}
</textarea>