// All rights reserved by panqiaomu from: http://blog.csdn.net/panqiaomu
// The heap and stack are different data structures ....
// Heap must be always dynamic but the stack can be static with array or dynamic with linklist...
// Heap has no fixed order but the stack must obey the rule: Filo
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
// The storage structure:
Typedef struct {
Char * Ch;
Int length;
} * Heapstr;
// Create a "string variable" (memory space) for a String constant;
Void strassign (heapstr S, char * strconst ){
Int Len = strlen (strconst );
If (S-> CH) Free (S-> CH );
If (! Len ){
S-> CH = NULL;
S-> length = 0;
}
Else {
S-> CH = (char *) malloc (LEN + 1); // + 1
If (! S-> CH)
Exit (0 );
Strcpy (S-> CH, strconst );
S-> length = Len;
}
}
// Insert a string into another before position POS which bases on the whole operation;
Void inserstr (heapstr S, int POs, heapstr t ){
Int I;
Int Len = T-> length;
If (Pos <1 | POS> S-> Length + 1)
Exit (0 );
If (t-> CH) {// or T-> length;
If (! (S-> CH = (char *) realloc (S-> CH, S-> Length + T-> length )))
Exit (0 );
// "Tim, fall back ";
For (I = s-> length; I> = pos-1; -- I) // include the '/0' so beginning with S-> length not-1 ....
S-> CH [I + Len] = s-> CH [I];
// Begin inserting the char streams;
// Don't call the function strcpy and copy the '/0' to string s which is wrong;
For (I = 0; I <Len; ++ I)
S-> CH [pos-1 + I] = T-> CH [I];
S-> Length + = Len;
}
}
// Return the length of the string
Int strlen (heapstr s ){
Return S-> length;
}
// Clear the string to empty (null );
Void clearstr (heapstr s ){
If (S-> CH ){
Free (S-> CH );
S-> CH = NULL;
}
S-> length = 0;
}
// ** Important OP: the match of strings beteen the main string and mode string;
// ** The classic one: String-search thought that we will realize it as follows;
// ** The rest ones: KMP and its progress, BM, Kr and so on
Int patmatch (heapstr MS, heapstr Pat, int POS ){
Int I, J;
If (Pos <1 | POS> MS-> length) // match after 'pos ';
Exit (0 );
I = pos-1;
J = 0;
While (I <MS-> length & J <pat-> length ){
If (MS-> CH [I] = pat-> CH [J]) {
++ I;
++ J;
}
Else {// I and j "fall back", the worst situation is O (S-> length * m-> length );
I = I-j + 2;
J = 1;
}
} // While
If (j> = pat-> length)
Return I-pat-> Length + 1;
Else
Return 0; // failure and return 0;
}
// Replace the substring sub with string target in string MS;
Void replacestr (heapstr MS, heapstr sub, heapstr TAR ){
Int Pos = 1;
Int I;
If (! Tar-> CH) return;
Pos = patmatch (MS, sub, POS );
While (POS ){
If (tar-> length <= sub-> length ){
For (I = 0; I <tar-> length; ++ I) // replace...
MS-> CH [pos-1 + I] = tar-> CH [I];
If (tar-> length! = Sub-> length) // not equal that is to say: <
For (I = pos-1 + sub-> length; I <= MS-> length; ++ I) // move to the front ....
MS-> CH [I-Sub-> Length + tar-> length] = MS-> CH [I];
MS-> length-= (sub-> length-tar-> length );
} // If
Else {
MS-> CH = (char *) realloc (MS-> CH, MS-> Length + tar-> length-Sub-> length); // reallocate... not + 1
If (! MS-> CH)
Exit (0 );
For (I = MS-> length; I >=pos-1 + sub-> length; -- I) // move to the back ....
MS-> CH [I + tar-> length-Sub-> length] = MS-> CH [I];
For (I = 0; I <tar-> length; ++ I) // replace ....
MS-> CH [I + pos-1] = tar-> CH [I];
MS-> Length + = (tar-> length-Sub-> length );
} // Else
Pos + = tar-> Length + 1;
Pos = patmatch (MS, sub, POS );
}
}
// Request the substring from the position 'pos' with the length 'len'
Void substr (heapstr sub, heapstr S, int POs, int Len ){
If (Pos <1 | POS> S-> length | Len <0 | Len> S-> length-pos + 1)
Exit (0); // make sure the strenth of codes;
If (sub-> CH) Free (sub-> CH );
If (! Len) {// request an empty string;
Sub-> CH = NULL;
Sub-> length = 0;
}
Else {
If (! (Sub-> CH = (char *) malloc (LEN + 1 )))
Exit (0 );
Strncpy (sub-> CH, S-> CH + pos-1, Len); // call function strncpy ();
Sub-> CH [Len] = '/0 ';
Sub-> length = Len;
}
}
// Begin testing in function main ()
Int main (){
Int Pos;
Heapstr R, S, T, sub;
Strassign (S, "Hello, my name is panqiaomu and your name? ");
Strassign (T, "qiaomu ");
Strassign (R, "mumumumumu ");
Strassign (sub, ""); // (sub, null)
Printf ("Call the function inserstr () ....../N ");
Inserstr (s, 7, t );
Printf ("the inserted string is: % s/n", S-> CH );
Printf ("The MS string length is: % d/N", strlen (s ));
Printf ("Call the function replacestr () ....../N ");
Replacestr (S, T, R );
Printf ("the replaced string is: % s/n", S-> CH );
Printf ("Call the function substr () ....../N ");
Substr (sub, s, 3, 3 );
Printf ("the substring is:/n % s/n", sub-> CH );
Getch ();
Return 0;
}
/* Summary: (1) do not wait until all the sub-functions are written before testing in the main function. This will cause a lot of trouble and should be tested in time after each module is written.
(2) do not start programming as soon as possible. First, there should be a general framework in the brain to solve the problem, which should be comprehensive, including all possible situations.
(3) When converting a pointer to an array, note that the subscript of the array starts from 0, which is the most annoying and annoying in C language.
(4) apply for space for strings. note when to add 1 and when not to add 1. For example, if you want to create a string variable to add 1, other operations such as replacement and insertion
No, because '/0' already exists in the original string '.*********************/