串的操作程序出现了错误?
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSTRLEN 255
typedef int Status;
typedef unsigned char SString[MAXSTRLEN+1];
Status StrAssigned(SString &T,char *chars)
{
int i=0;
int len;
len=strlen(chars);
if(strlen(chars)>MAXSTRLEN)
{
return ERROR;
}
else
{
T[0]=strlen(chars);
for(i=1;i<=T[0];i++)
{
T[i]=chars[i-1];
}
}
return OK;
}
Status StrConcat(SString &T,SString S1,SString S2)
{
int i=0;
if(S1[0]+S2[0]<=MAXSTRLEN)
{
T[0]=S1[0]+S2[0];
for(i=1;i<=S1[0];i++)
{
T[i]=S1[i];
}
for(i=1;i<=S2[0];i++)
{
T[S1[0]+i]=S2[i];
}
return OK;
}else if(S1[0]>MAXSTRLEN&&S1[0]+S2[0]>MAXSTRLEN)
{
T[0]=MAXSTRLEN;
for(i=1;i<=S1[0];i++)
{
T[i]=S1[i];
}
for(i=1;i<=MAXSTRLEN-S1[0];i++)
{
T[S1[0]+i]=S2[0];
}
return OK;
}
else
{
T[0]=MAXSTRLEN;
for(i=0;i<MAXSTRLEN;i++)
{
T[i]=S1[0];
}
}
return OK;
}
Status SubString(SString &Sub,SString S,int pos,int len)//这个函数当len大于pos的时候,运行的结果出现了乱码字符,程序改如何修改?
{
int i,j=1;
if(pos<1||pos>S[0])
{
return ERROR;
}
if(len>S[0]-pos+1)
{
return ERROR;
}
for(i=pos;i<=len+len-pos;i++)
{
Sub[j]=S[j];
j++;
}
Sub[0]=len;
return OK;
}
Status StrPrint(SString T)
{
int i=0;
for(i=1;i<=T[0];i++)
{
printf("%c",T[i]);
}
printf("\n");
return OK;
}
int main(int argc, char* argv[])
{
int pos,len,k;
SString T,Sub,S1,S2;
SString S;
char chars[20];
printf ("input S1:\n");
scanf ("%s",chars);
StrAssigned(S1,chars);
fflush(stdin);
printf("input S2:\n");
scanf("%s",chars);
StrAssigned(S2,chars);
fflush(stdin);
printf("after lianjie:\n");
StrConcat(T,S1,S2);
StrPrint(T);
printf("\n");
printf ("input a string S:\n");
scanf ("%s",chars);
StrAssigned(S,chars);
printf("input pos:\n");
scanf("%d",&pos);
printf("input len:\n");
scanf("%d",&len);
SubString(Sub,S,pos,len);
StrPrint(Sub);
return 0;
}