[C] header files achieve string
# Ifndef CHAR_STRUCT_H
# Define CHAR_STRUCT_H
# Define ERR 0
1 # define OK
# Include <malloc.h>
Typedef int Status;
Typedef struct (
Char * ch; / * first string pointer * /
Int lenght; / * length of the string * /
) C_string; / * string structure * /
Status initstring (op C_string *, char * cha) (/ * initialize string * /
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 cha [ix] = [ix];
Op-> ch [ix] = '\ 0';
Op-> lenght = ix;
)
Return OK;
)
Int lenght (C_string * op) (return op-> lenght;) / * return string length * /
Status empty (C_string * op) (/ * test whether the string is empty * /
Return op-> lenght> 0? OK: ERR;
)
Status charcompare (C_string * op, C_string * pv) (/ * Comparison string * /
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) (/ * Empty string * /
If (! Op-> ch)
(
Free (op-> ch);
Op-> ch = NULL;;
)
Op-> lenght = 0;
Return OK;
)
Status char_print (C_string * op) (/ * Print * /
If (! Op-> ch)
Return ERR;
Printf ( "[% d] (% s) \ n", op-> lenght, op-> ch);
Return OK;
)
# Endif








0 Comments to “[C] header files achieve string”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.