#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 64
char *mystrstr(char *src, char *needle);
int main()
{
FILE *fp;
char buff[N];
char buff1[N];
char needle[N];
// char *needle = "aac";
fp=fopen("cover.txt","r");
int count=0;
gets(buff); //此处为临时测试用,实际中不允许使用危险的gets函数
while(fgets(needle,N,fp))
{
needle[strlen(needle)-1]='\0';
strcpy(buff1,buff);
mystrstr(buff,needle); //调用子函数处理字符串
if(strcmp(buff1,buff)!=0)
{
count++;
printf("%d",count);
break;
}
}
puts(buff); //打印处理后的结果
return 0;
}
char *mystrstr(char *src, char *needle)
{
char *old = src;
int len = 0;
int i = 0;
if(NULL == src || NULL == needle) //安全校验,如果指针非法则退出
return NULL;
while('\0' != *src) //进入主循环,条件为字符串的结束标识
{
len = 0;
while(*src != *needle && *src != '\0') //第一步,先找到要屏蔽的字符串头
src ++;
while(src[len] == needle[len] && needle[len] != '\0') //第二步,找到字符串尾
len ++;
if(needle[len] == '\0') //依据条件判断,如果找到了,则开始屏蔽
{
for(i = 0; i < len; i ++)
src[i] = '*';
src += len;
}
else
src ++; //否则从下一个字符开始重新查找。
}
return old;
}
找自互联网,有问题请及时联系我。
屏蔽字然后使用*替换(转)
最新推荐文章于 2023-07-03 09:53:56 发布