#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define OVERFLOW 0
#define OK 1
typedef struct
{
char *ch;
int length;
}HString;
int StrAssign ( HString &T, char chars[] )
{
//生成一个其值等于串常量chars的串T
char *c;
int i;
int j;
if ( T.ch )
free ( T.ch );
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 ( j = 0; j < i; j++ )
{
T.ch[j] = chars[j];
T.length++;
}
}
return OK;
}
/*
void String_Reverse ( HString T, HString &r )
{
//求s的逆串r
int i;
int j;
char *s = NULL;
StrAssign(r,s); //初始化r为空串
for ( i = T.length; i > 0; i-- )
{
*r.ch++ = T.ch[i];
r.length++;
}
for ( j = 0; j < r.length; j++ )
printf("s的逆串r==>%c", r.ch[j]);
}
*/
void main()
{
HString T;
//
HString r;
char *chars = "I love China";
StrAssign ( T, chars );
//
String_Reverse ( T, r );
}