#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
串的模式匹配(用一般算法实现)
最新推荐文章于 2021-06-29 16:05:21 发布
这篇博客介绍如何使用C语言实现串的模式匹配算法。通过创建`CommonStr`结构体来存储字符串,`CreateStr`函数生成测试字符串,`DisplayStr`函数打印字符串,`Index`函数进行模式匹配。程序随机生成主串和模式串,并允许用户指定从主串的哪个位置开始查找模式串,最后输出匹配结果。
摘要由CSDN通过智能技术生成