【头文件】c实现字符串

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

#ifndef CHAR_STRUCT_H

#define CHAR_STRUCT_H

#define ERR 0

#define OK 1

#include <malloc.h>

typedef int Status;

typedef struct {

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

int lenght;/*字符串长度*/

} C_string;/*字符串结构 */

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;

}

#endif

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航