[Library] C to achieve strings

zhaozj2021-02-16  78

_________________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 stat;

Typedef struct {

Char * ch; // Character serial pointer

INT belnet; // String length

} C_string; // string structure

Status INITSTRING (c_string *, char *); // Initialization string

INT Lenght (c_string *); // Returns the length of the string

Status Empty (c_string *); // Test if the string is empty

Status Charcompare (c_string *, c_string *); // Compare strings

STATUS CLS (c_string * OP); / / Clear string

Status char_print (c_string * OP); // Print

#ENDIF

___________________ String.c ___________________________

#include

#include

#include "char_struct.h"

Status INITSTRING (C_String * Op, Char * CHA)

{/ * Initialized string * /

IX;

CHAR * C = CHA;

IF (! OP-> ch)

Free (OP-> CH);

For (ix = 0; * C! = '/ 0'; ix, c);

IF (ix == 0)

{

Op-> ch = null;

OP-> Lenet = 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-> Lenet = IX;

}

Return OK;

}

INT Lenght (c_string * OP)

{

Return OP-> Lenet;

} / * Returns the length of the string * /

Status empty (c_string * OP) {/ * test string is empty * /

RETURN OP-> LengHT> 0? OK: ERR;

}

Status Charcompare (C_String * OP, C_String * PV) {/ * Comparison String * /

IX;

IF (op-> length == 0 || Pv-> LengHT == 0)

Return ERR;

IF (op-> length! = pv-> length)

Return ERR;

For (ix = 0; ix length; ix)

IF (OP-> CH [IX]! = PV-> CH [IX])

Return ERR;

Return OK;

}

STATUS CLS (c_string * OP) {/ * Clear string * / if (! OP-> CH)

{

Free (OP-> CH);

Op-> ch = null ;;

}

OP-> Lenet = 0;

Return OK;

}

Status char_print (c_string * OP) {/ * Print * /

IF (! OP-> ch)

Return ERR;

Printf ("[% D] (% s) / n", op-> lengtht, op-> ch);

Return OK;

}

转载请注明原文地址:https://www.9cbs.com/read-12379.html

New Post(0)