![]() |
#2
chuanglan2012-11-23 20:27
找到了,是插入函数里面没给S->len改变值,因此S->ch的实际长度没有那么多额
,还有我的删除函数也有问题现附上修改好的代码,谢谢各位的关注额, ![]() #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; 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]; } S->len = S->len + Lc; } int StrMatch(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) { *pos = i + 1; return 1; } } if(i > S.len-comlen) puts("We didn't find the string!"); return 0; } int Delete(String *S,char delstr[]) { int delen = strlen(delstr); int i,pos; if(!StrMatch(*S,delstr,&pos)) return 0; for(i = 0; pos < S->len; i++) { S->ch[pos-1] = S->ch[pos-1+delen]; pos ++; } } void main() { String S; char chars[string_size]; char T[string_size]; char delstr[string_size]; char comstr[string_size]; int pos; // 字符串初始化与插入 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); if(StrMatch(S,comstr,&pos)) printf("the postion of the comstring is %d to %d\n",pos,pos+strlen(comstr)-1); */ // 字符串的删除 puts("Please input the string to del"); scanf("%s",delstr); if(Delete(&S,delstr)) printf("The deleted string is:\n%s",S.ch); getch(); } |

#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();
}
#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();
}