In C and C ++ languages, a shared space called a heap is provided. During the program running, the system uses the functions malloc () and free () dynamically apply for or release a continuous space.
In C and C ++, pointers can be used to access and operate arrays. In addition, the above features can be fully utilized in string storage and operations.
The Heap Storage Structure of strings is similar to the sequential storage structure of a linear table. It stores string-value character sequences in a group of sequential storage units. Its storage space is dynamically allocated during program execution, instead of static allocation.
Stack Storage Structure Definition of strings
Typedef struct {char * ch; // pointer field, pointing to the base address of the bucket storing string values int length; // integer field: storage String length} HString;
Note: In this storage structure, because strings are still represented by character sequences stored in arrays, the operations on these strings first allocate a storage space for newly generated strings, then, copy the character sequence ". For example, if the StrInsert (S, pos, T) (before inserting string T to the pos character of string S) algorithm is, re-allocate the bucket with the size equal to the sum of string S and string T Length for string S, and then copy the S and T string values to the newly allocated bucket.
A series of implementations of the string www.2cto.com
/*
Heap Storage representation of strings
*/
# Include <stdio. h>
# Include <stdlib. h>
# Define OVERFLOW-2 // Heap OVERFLOW
# Define OK 1 // The function is successfully executed.
# Define ERROR-1 // function execution failed
Typedef int datatype;
Typedef struct {
Char * ch; // pointer field, pointing to the base address of the bucket storing string values
Int len; // integer field: String Length
} HString;
// Initialize an empty string
Datatype InitHString (HString * s)
{
S-> ch = (char *) malloc (sizeof (char ));
If (NULL = s-> ch) return OVERFLOW;
S-> ch = NULL;
S-> len = 0;
Return OK;
}
// Assign values to strings
Datatype assigment_string (HString * s, const char * str)
{
If (s-> ch) free (s-> ch );
Int length = 0, I;
While (str [length]! = '\ 0 ')
Length ++;
S-> ch = (char *) malloc (length * sizeof (char ));
If (! S-> ch) return OVERFLOW;
For (I = 0; I <length; I ++)
* (S-> ch + I) = * (str + I );
S-> len = length;
* (S-> ch + s-> len) = '\ 0 ';
Return OK;
}
// Print
Void PrintString (const HString * s)
{
Int I = 0;
While (* (s-> ch + I )! = '\ 0 ')
{
Printf ("% c", * (s-> ch + I ));
I ++;
}
Printf ("\ n ");
}
// Evaluate the length of a string
Datatype GetLength (const HString * s)
{
Int len = 0;
While (* (s-> ch + len )! = '\ 0 ')
Len ++;
Return len;
}
// Locate a character and return the order
Datatype HSLocal (const HString * s, char c)
{
Int I;
For (I = 0; I <s-> len; I ++)
{
If (c = * (s-> ch + I ))
Return I + 1;
}
Return-1;
}
// String connection
Datatype HSCat (HString * s, const HString * t)
{
Int I = 0;
Char * temp = (char *) malloc (s-> len + t-> len) * sizeof (char ));
If (! Temp) return OVERFLOW;
For (I = 0; I <s-> len; I ++)
* (Temp + I) = * (s-> ch + I );
Free (s-> ch); // release to prevent memory leakage
For (I = 0; I <t-> len; I ++)
* (Temp + I + s-> len) = * (t-> ch + I );
S-> len + = t-> len;
S-> ch = temp; // make s-> ch point to temp again
* (S-> ch + s-> len) = '\ 0 ';
Return OK;
}
Datatype HSCopy (HString * to, const HString * from)
{
// Empty if the target string is not empty
[Cpp] view plaincopy
If (to-> ch)
{
Free (to-> ch );
To-> ch = NULL;
To-> len = 0;
}
Int I = 0;
To-> len = from-> len;
To-> ch = (char *) malloc (from-> len * sizeof (char ));
If (! To-> ch) return OVERFLOW;
For (I = 0; I <from-> len; I ++)
* (To-> ch + I) = * (from-> ch + I );
* (To-> ch + to-> len) = '\ 0 ';
Return OK;
}
// Insert string t at the pos position of string s
Datatype HSInsert (HString * s, const HString * t, int pos)
{
If (pos <0 | pos> s-> len)
Return ERROR;
Else {
S-> ch = (char *) realloc (s-> ch, (s-> len + t-> len) * sizeof (char ));
Int I, j = 1;
// Move back
For (I = s-> len + t-> len-1; I> = pos + t-> len; I --)
{
* (S-> ch + I) = * (s-> ch + (s-> len-j ));
J ++;
}
// Insert
For (I = 0; I <t-> len; I ++)
* (S-> ch + I + pos) = * (t-> ch + I );
}
S-> len + = t-> len;
* (S-> ch + s-> len) = '\ 0 ';
Return OK;
}
// Delete the index of string s with a length of x characters
Datatype HSdel (HString * s, int index, int x)
{
// Index value invalid
If (index <0 | index> s-> len) return ERROR;
// If x + index> s-> len
If (index + x)> = s-> len)
S-> len = index;
Else
{
Int I;
For (I = index; I <s-> len; I ++)
{
* (S-> ch + I) = * (s-> ch + I + x );
}
S-> len-= x;
}
* (S-> ch + s-> len) = '\ 0 ';
Return OK;
}
// String comparison
Datatype HScomp (const HString * s1, const HString * s2)
{
Int I = 0;
For (I = 0; I <s1-> len & I <s2-> len; I ++)
{
If (* (s1-> ch + I )! = * (S2-> ch + I ))
Return * (s1-> ch + I)-* (s2-> ch + I );
}
Return 0; // equal
}
/* String Extraction
* The Child string whose index start length is length in string s is extracted to the temp string.
**/
Datatype HSsub (HString * temp, const HString * s, int index, int length)
{
If (index <0 | index> s-> len) return ERROR;
If (length> s-> len) length = s-> len-index;
Temp-> len = length;
Temp-> ch = (char *) malloc (length * sizeof (char ));
If (! Temp-> ch) return OVERFLOW;
Int I;
For (I = 0; I <length; I ++)
{
* (Temp-> ch + I) = * (s-> ch + index + I );
}
* (Temp-> ch + temp-> len) = '\ 0 ';
Return OK;
}
// Replace the string. Replace the string t with the substring whose index length is length.
Datatype HSReplace (HString * s, int index, int length, const HString * t)
{
If (index <0 | index> s-> len)
Return ERROR;
// If the length is greater than the length of the string s, all characters after the index are replaced.
If (length> s-> len)
Length = s-> len;
Int I;
For (I = 0; I <length; I ++)
{
* (S-> ch + I + index) = * (t-> ch + I );
}
* (S-> ch + s-> len) = '\ 0 ';
Return OK;
}
Int main (int argc, char ** argv)
{
// Bulid string
HString S;
InitHString (& S );
Assigment_string (& S, "hello world! ");
PrintString (& S );
Printf ("length = % d \ n", GetLength (& S ));
# If 0
// Localion
Int local = HSLocal (& S, 'w ');
Printf ("local = % d \ n", local );
Free (S. ch );
# Endif
# If 0
// Insert
HString S1;
InitHString (& S1 );
Assigment_string (& S1 ,"****");
HSInsert (& S, & S1, 2 );
PrintString (& S );
Printf ("length = % d \ n", GetLength (& S ));
Free (S1.ch );
# Endif
# If 0
// Copy
HString S2;
InitHString (& S2 );
Assigment_string (& S2, "beijing ");
HSCopy (& S, & S2 );
PrintString (& S );
Printf ("length = % d \ n", GetLength (& S ));
Free (S. ch );
Free (S2.ch );
# Endif
# If 0
// Cat
HString S3;
InitHString (& S3 );
Assigment_string (& S3 ,"////");
HSCat (& S, & S3 );
PrintString (& S );
Printf ("length = % d \ n", GetLength (& S ));
Free (S3.ch );
Free (S. ch );
# Endif
# If 0
// Delete
HSdel (& S, 2, 5 );
PrintString (& S );
Printf ("length = % d \ n", GetLength (& S ));
Free (S. ch );
# Endif
# If 0
// Complar
HString S4;
InitHString (& S4 );
Assigment_string (& S4, "hello world! ");
Datatype ret = HScomp (& S, & S4 );
If (ret> 0)
Printf ("S> S4 \ n ");
Else if (ret <0)
Printf ("S <S4 \ n ");
Else
Printf ("S = S4 \ n ");
Free (S4.ch );
Free (S. ch );
# Endif
# If 0
// Sub
HString S5;
HSsub (& S5, & S, 2, 5 );
PrintString (& S5 );
Printf ("length = % d \ n", GetLength (& S5 ));
Free (S. ch );
Free (S5.ch );
# Endif
# If 1
// Replace
HString S6;
InitHString (& S6 );
Assigment_string (& S6, "************************************");
HSReplace (& S, 2, 50, & S6 );
PrintString (& S );
Printf ("length = % d \ n", GetLength (& S ));
Free (S6.ch );
Free (S. ch );
# Endif
// Free (S. ch );
Return 0;
}