_________________string____________________

#ifndef CHAR_STRUCT_H

#define CHAR_STRUCT_H

#ifndef ERR

#define ERR -1

#define OK 1

#endif

#ifndef MIN

#define MIN 0

#define MAX 100

#endif

typedef int status;

typedef struct {

    char *ch;//字符串头指针

    int  lenght;//字符串长度

} C_string;//字符串结构

status initstring( C_string*, char* );//初始化字符串

int lenght( C_string* );//返回字符串长度

status empty ( C_string* );//测试字符串是否为空

status charcompare( C_string*, C_string* );//比较字符串

status cls( C_string *op );//清空字符串

status char_print( C_string *op );//打印

#endif

___________________string.c____________________________

#include <stdio.h>

#include <malloc.h>

#include "char_struct.h"

status initstring( C_string *op, char *cha )

{/*初始化字符串*/

    int ix;

    char *c = cha;

    if( !op->ch )

       free( op->ch );

    for( ix = 0; *c != '/0' ; ++ix, ++c );

    if( ix == 0 )

    {

       op->ch = NULL;

       op->lenght = 0;

    }

    else

    {

       op->ch = ( char* ) malloc ( ix * sizeof( char ) );

       if( !op->ch )

           return ERR;

       for( ix = 0; cha[ ix ] != '/0'; ++ix )

           op->ch[ ix ] = cha[ ix ];

       op->ch[ ix ] = '/0';

       op->lenght = ix;

    }

    return OK ;

}

int lenght( C_string *op )

{

    return op->lenght;

}/*返回字符串长度*/

status empty ( C_string *op ) { /*测试字符串是否为空*/

    return op->lenght > 0 ? OK : ERR;

}

status charcompare( C_string *op, C_string *pv ){/*比较字符串*/

    int ix;

    if( op->lenght == 0 || pv->lenght == 0 )

       return ERR;

    if( op->lenght != pv->lenght )

       return ERR;

    for( ix = 0; ix < op->lenght; ++ix )

       if( op->ch[ ix ] != pv->ch[ ix ] )

           return ERR;

    return OK;

}

status cls( C_string *op ){/*清空字符串*/

    if( !op->ch )

    {

       free( op->ch );

       op->ch = NULL;;

    }

    op->lenght = 0;

    return OK;

}

status char_print( C_string *op ) {/*打印*/

    if( !op->ch )

       return ERR;  

    printf( "[ %d ] ( %s )/n", op->lenght, op->ch );

    return OK;

}