问题描述
- 如何用串的堆分配存储方法实现串Index( )函数
- 我的代码如下:(问题:代码实现不了功能)
#include
#include
#include#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int Boolean;typedef int ElemType;
typedef struct {
char *ch;
int length;
}HString;Status StrAssign(HString &Tchar chars) {
if (T.ch) free(T.ch);
int i;char *c;
for (i=0c=chars; *c; ++i++c);
if (!i) { T.ch =NULL; T.length =0;}
else {
if (!(T.ch = (char)malloc(i* sizeof(char))))
exit (OVERFLOW);
for(int j=0;j<i;j++)
T.ch[j]=chars[j];
T.length = i;
}
return OK;
}Status InitString(HString &S) {
S.ch = NULL;S.length = 0;
return OK;
}int Index(HString SHString Tint pos) {
if (posS.length)
return ERROR;
int ij;
i=pos; j=1;
while(i<=S.length && j<=T.length) {
if(S.ch[i]==T.ch[j]) {++i; ++j;}
else {i=i-j+2;j=1;}
}
if(j>T.length) return i-T.length;
else return 0;
}Status dispstr(HString S) {
if (S.length==0) return ERROR;
int i;for (i=0;i<S.length;i++) printf(""%c""S.ch[i]);
printf(""n"");
return OK;}
void main()
{
HString st;
InitString(s);
InitString(t);
StrAssign(sabcabcdabcdeabcdefabcdefg"");
StrAssign(tabcdeabcdefab"");
dispstr(s);
dispstr(t);
printf(""%dn""Index(st1));
}