串的模式匹配(用一般算法实现)

这篇博客介绍如何使用C语言实现串的模式匹配算法。通过创建`CommonStr`结构体来存储字符串,`CreateStr`函数生成测试字符串,`DisplayStr`函数打印字符串,`Index`函数进行模式匹配。程序随机生成主串和模式串,并允许用户指定从主串的哪个位置开始查找模式串,最后输出匹配结果。
摘要由CSDN通过智能技术生成
#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define ElemType char

typedef struct
{
	ElemType *start;//顺序字符串的起始位置
	int length;//字符串的长度,即串中的字符个数
}CommonStr;

int CreateStr(CommonStr &pstr, int n)//生成用于测试的主串
{
	pstr.start=(ElemType *)malloc(100*sizeof(ElemType));
	int i,k=0;
	pstr.length=0;
	srand((unsigned)time(NULL));
	for(i = 0; i < n; i++)
	{
		pstr.start[i]=(char)(rand()%2 + 'a');
		pstr.length++;
	}
	pstr.start[i]='\0';
	return 1;
}

int DisplayStr(CommonStr str)//打印字符串
{
	for(int i=0;i<str.length-1;i++)
	{//打印前面的字符
		printf("(%3d,%c) ",i+1,str.start[i]);
	}
	printf("(%3d,%c)\n\n",i+1,str.start[i]);//打印最后一个字符
	return 1;
}

int Index(CommonStr pstr,CommonStr mstr)
{
	int i,pos,j=0;
	printf("你要从主串的哪个位置开始查找,请输入:");//要求开始位大于等于1
	scanf("%
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Storm-Shadow

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值