请求字符串暴力匹配代码修正方案

这个代码运行出问题,有没有大神看看哪儿出错了啊(小白求助)

//字符串暴力匹配
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//**********************************************************************
typedef char DATA;
//**********************************************************************
int BF(char a[],char b[]);
void ceshi();
//***********************************************************************算法
int BF(char a[],char b[]){
	int i=0,j=0;
	printf("the host string\n");
	printf("%s\n",a);
	printf("the second string\n");
	printf("%s\n",b);
	while(a[i]!='\0'&&b[j]!='\0'){//两个字符串结束时退出循环
		if(a[i]==b[j]){
			i++;//主串的数组位置
			j++;//副串的数组位置
		}
		else{
			i=i-j+1;//主串位置回溯
			j=0;//副串数组位置返回开头
			}
		if(a[i]=='\0'){
			printf("\n");
			printf("It's successful\n");
			return (i-j);//返回主串数组位置
		}
		if(b[j]=='\0'){
			printf("\n");
			printf("error!!!\n");
			break;//退出循环
		}
	}
	printf("\n");
	return -1;//返回-1
}
//**************************************************************通过动态数组的形式把输入的数组返回给bf函数
char *shuzu(char *str){
	str=(char *)malloc(sizeof(char));
	int len;
	if(str==NULL)
		exit(-1);
	scanf("%s",str);
	getchar();
	len=strlen(str);
	printf("this is your string\n%s,and your string lenth is %d\n",str,len);
	printf("\n");
	return str;
	free(str);
}
//***************************************************************
void ceshi(){
	char *a=NULL,*b=NULL;
	printf("please scanf your host string\n");
	a=shuzu(a);//输入主串
	printf("\n");
	printf("please scanf your second string\n");
	b=shuzu(b);//输入副串
	int i;
	i=BF(a,b);//通过bf函数进行匹配,然后返回主串中副串的数组位置
	printf("%d\n",i);
}
//*********************************************************
int main(){
	void ceshi();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值