昵称怎么起 2013-11-13 13:18
浏览 1497

如何用串的堆分配存储方法实现串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 &T,char chars) {
if (T.ch) free(T.ch);
int i;char *c;
for (i=0,c=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 S,HString T,int pos) {
if (posS.length)
return ERROR;
int i,j;
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 s,t;
InitString(s);
InitString(t);
StrAssign(s,"abcabcdabcdeabcdefabcdefg");
StrAssign(t,"abcdeabcdefab");
dispstr(s);
dispstr(t);
printf("%d\n",Index(s,t,1));
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 matlab解优化问题代码
    • ¥15 写论文,需要数据支撑
    • ¥15 identifier of an instance of 类 was altered from xx to xx错误
    • ¥100 反编译微信小游戏求指导
    • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
    • ¥15 学不会递归,理解不了汉诺塔参数变化
    • ¥15 基于图神经网络的COVID-19药物筛选研究
    • ¥30 软件自定义无线电该怎样使用
    • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
    • ¥15 Jenkins+k8s部署slave节点offline