字符串匹配出现了BUG,怎么看都不明白哪错了,各位帮我看看吧
程序代码:#include<stdio.h>
#include<string.h>
#define string_size 500
typedef struct
{
char ch[string_size];
int len;
}String;
void StrAsign(String *S,char chs[])
{
char *q = S->ch;
/* q = (char *)malloc(sizeof(string_size));*/
strcpy(q,chs);
S->len = strlen(chs);
}
void StrInsert(String *S,int pos,char chs[])
{
int La,Lb,Lc;
int RemainSize,i;
char B[string_size];
La = pos;
Lb = S->len - pos;
Lc = strlen(chs);
if( La+Lb+Lc <= string_size )
{
for(i = 0; i <= Lb; i++)
B[i] = S->ch[pos+i-1];
for(i = 0; i < Lc; i++)
S->ch[pos+i-1] = chs[i];
for(i = 0;i <= Lb; i++)
S->ch[i+pos+Lc-1] = B[i];
}
else if(Lb+Lc < string_size)
{
RemainSize = string_size-(La+Lc);
for(i = 0; i < RemainSize; i++)
B[i] = S->ch[pos+i];
for(i = 0; i < Lc; i++)
S->ch[pos+i] = chs[i];
for(i = 0;i < RemainSize; i++)
S->ch[i+pos+Lc] = B[i];
}
else
{
RemainSize = string_size - La;
for(i = 0;i < RemainSize; i++)
S->ch[pos+i] = chs[i];
}
}
void Compare(String S,char comstr[],int *pos) // 匹配函数,单词用错了,别踩我额,嘿嘿
{
int i,j,start;
int comlen;
comlen = strlen(comstr);
for(i = 0; i < S.len-comlen; i++) //用的是比较差的算法,每次从主串的下一个字符开始匹配
{
start = i;
for(j = 0; j < comlen; j++) // 每次遇到不能匹配的字符则终止循环,问题好像就出在这里,但真不知道为什么....
{
if(S.ch[start] != comstr[j])
break;
start++;
}
if(j >= comlen)
{
printf("It's the i value %d\n",i);
*pos = i;
printf("This is the pos's value %d\n",*pos);
break;
}
}
}
void Delete(String *S,char delstr[])
{
int delen = strlen(delstr);
int i,pos;
Compare(*S,delstr,&pos);
for(i = 0;i < delen; i++)
{
S->ch[pos] = S->ch[pos+delen];
pos ++;
}
}
void main()
{
String S;
char chars[string_size];
char T[string_size];
char delstr[string_size];
char comstr[string_size];
int pos,pos1;
puts("Pls input the string and inserted str and it's position");
scanf("%s%s%d",chars,T,&pos);
StrAsign(&S,chars);
puts("Now we have gotten the string of S");
puts(S.ch);
StrInsert(&S,pos,T);
printf("after the insert String became as the following: %s\n",S.ch);
puts("please input the string to compare:");
scanf("%s",comstr);
Compare(S,comstr,&pos1);
printf("the postion of the comstring is %d\n",pos1);
/*
puts("Please input the string to del");
scanf("%s",delstr);
Delete(&S,delstr);
printf("The deleted string is:\n%s",S.ch); */
getch();
}






