如何让C语言编程结果不重复,C语言编程题,求助大神,麻烦给出代码的同时能有结果图,谢谢啦!!!...

本文介绍了如何将整数转换为字符序列,并运用KMP算法进行字符串搜索。通过`itoa`函数实现十进制转字符,然后利用`next_generate`辅助KMP算法在文件中查找指定模式。主要关注了字符串处理和算法优化。
摘要由CSDN通过智能技术生成

#include

#include

#define MAXSIZE 20

void swap(char *a, char *b)

{

char temp=*a;

*a=*b;

*b=temp;

}

char * itoa(int num)

{

char * strNum=(char *)calloc(MAXSIZE,sizeof(char));

int i,j;

do

{

strNum[i++]='0'+num%10;

num/=10;

} while (num);

strNum[i]='\0';

for(--i,j=0;j

return strNum;

}

int * next_generate(char * p)

{

int * pnext=(int *)calloc(MAXSIZE,sizeof(int));

pnext[0]=-1;

int k,j;

for(k=-1,j=0;p[j];)

{

if(k==-1||p[j]==p[k])

{

j++;

k++;

pnext[j]=k;

}

else

k=pnext[k];

}

return pnext;

}

void KMP(FILE * IN, char * p)

{

int *Jnext=next_generate(p);

int j=0,row=0,col=0;

char c=fgetc(IN);

while(c!=EOF&&p[j])

{

if(j==-1||p[j]==c)

{

j++;

c=fgetc(IN);

col++;

if(c=='\n')

{

col=0;

row++;

}

}

else

{

j=Jnext[j];

}

}

if(!p[j])

{

printf("I find it,below is its position\nrow:%d\ncol:%d",row+1,col-j+1);

return;

}

printf("There is no such number in the file");

}

int main()

{

FILE * ptr=fopen("data.txt","r");

char * strNum=itoa(100);

int * a=next_generate(strNum);

KMP(ptr,strNum);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值