Brute-Force algorithm for string search in c Data Structure

Source: Internet
Author: User

Brute-Force algorithm for string search in c Data Structure
# Include
# Include
# Include


// Define the struct of the string
Typedef struct {


Char * str; // string
Int maxLength; // The maximum length of characters that can be stored.
Int length; // The current character length.
} DString;




// 1. Initialization
// Initialization is used to create and store the dynamic array space of strings and assign values to related data domains
Void Initiate (DString * s, int max, char * string ){


Int I;
S-> str = (char *) malloc (sizeof (char) * max); // apply for a dynamic Bucket
S-> maxLength = max; // maximum number of dynamic array elements
S-> length = strlen (string); // sets the current length of the string.
For (I = 0; I Length; I ++) {// for Loop assignment

S-> str [I] = string [I]; // value assignment
}
}




// 2. Insert a substring
Int Insert (DString * s, int pos, DString t ){
// Insert the substring t at the pos position of the primary string s. If the insertion is successful, 1 is returned. If the insertion fails, 0 is returned.
Int I;
If (pos <0 ){

Printf ("pos location error, pos <0 \ n ");
Return 0;
} Else {
// If there is not enough space, the new space will be allocated.
If (s-> length + t. length> s-> maxLength ){

// Re-apply for the array space indicated by s-> str. The original array elements are stored before the new array.
Realloc (s-> str, (s-> length + t. length) * sizeof (char ));
S-> maxLength = s-> length + t. length; // The maximum length has changed.
}


For (I = s-> length-1; I> = pos; I --){
// Move the positions t. length in sequence
S-> str [I + t. length] = s-> str [I];
}


// Insert at the pos position
For (I = 0; I
S-> str [pos + I] = t. str [I]; // insert


}


S-> length = s-> length + t. length; // set the position of the new data element.
Return 1;
}
}






// 3. delete a substring
Int Delete (DString * s, int pos, int len ){
// Delete the sub-string whose length starts from the pos position of the primary string s is len. If the sub-string is deleted successfully, 1 is returned; otherwise, 0 is returned.
Int I;
If (s-> length <= 0 ){
Printf ("no characters in the array, no elements can be deleted! \ N ");
Return 0;
} Else if (pos <0 | len <0 | pos + len> s-> length ){

Printf ("the pos and len parameters are invalid! \ N ");
Return 0;
} Else {

For (I = pos + len; I <= s-> length-1; I ++ ){

S-> str [I-len] = s-> str [I]; // forward the len position in sequence
}


S-> length = s-> length-len; // set the number of new elements.
Return 1;
}




}






// 4. substring operation
Int SubString (DString * s, int pos, int len, DString * t ){
// Obtain the sub-string whose length starts from the pos position as len. If the string is successful, 1 is returned. If the string fails, 0 is returned.
Int I;
If (pos <0 | len <0 | pos + len> s-> length ){

Printf ("An error occurred while configuring the pos and len parameters !!! \ N ");
Return 0;
}
// When t space is insufficient, the new space will be allocated.
If (len> t-> maxLength ){

T-> str = (char *) realloc (t-> str, len * sizeof (char); // re-apply for array space
T-> maxLength = len;
}


For (I = 0; I
T-> str [I] = s-> str [pos + I]; // obtain the substring
}


T-> length = len;
Return 1;


}






// 5. Cancel the operation
Void Destroy (DString * s ){


// Cancel the memory space occupied by string s
Free (s-> str );
S-> maxLength = 0;
S-> length = 0;
}








// Brute-Force algorithm Function Design
// The I variable indicates the subscript of the current comparative character of the Main string s
// Use the j variable to represent the subscript of the current comparative character of the substring t
Int BFIndex (DString s, int start, DString t ){


Int I = start, j = 0, v;
While (I
If (s. str [I] = t. str [j]) {

I ++;
J ++;
} Else {

I = I-j + 1;
J = 0;
}


}


If (j = t. length ){

V = i-t.length;


} Else {

V =-1;
}


Return v;


}






Int main (){
/*
DString myString1, myString2, myString3;
Int I, max1 = 5, max2 = 9, max3 = 0;
// Test the initialization Function
Initiate (& myString1, max1, "Data ");
Initiate (& myString2, max2, "Structure ");
Initiate (& myString3, max3 ,"");


Printf ("initialize myString2 string :");
For (I = 0; I Printf ("% c", myString2.str [I]);
Printf ("maxLength = % d", myString2.maxLength );
Printf ("length = % d \ n", myString2.length );


// Test the insert Function
Insert (& myString2, 0, myString1 );
Printf ("myString2 string after inserting the substring :");
For (I = 0; I Printf ("% c", myString2.str [I]);
Printf ("maxLength = % d", myString2.maxLength );
Printf ("length = % d \ n", myString2.length );


// Test the deletion function
Delete (& myString2, 0, 5 );
Printf ("myString2 string after the substring is deleted :");
For (I = 0; I Printf ("% c", myString2.str [I]);
Printf ("maxLength = % d", myString2.maxLength );
Printf ("length = % d \ n", myString2.length );




// Test the substring Function
SubString (& myString2, 0, 5, & myString3 );
Printf ("myString3 string after substring :");
For (I = 0; I Printf ("% c", myString3.str [I]);
Printf ("maxLength = % d", myString3.maxLength );
Printf ("length = % d \ n", myString3.length );






////////////////////////////
For (I = 0; I Printf ("% c", myString2.str [I]);
Printf ("maxLength = % d", myString2.maxLength );
Printf ("length = % d \ n", myString2.length );




// Test the Undo function
// Destroy (& myString1 );
// Destroy (& myString2 );
// Destroy (& myString3 );
*/






DString myString1, myString2;
Int max1 = 29, max2 = 9;
Int pos = 0;
Initiate (& myString1, max1, "Data Structure ");
Initiate (& myString2, max2, "Structure ");
// First search
Pos = BFIndex (myString1, pos, myString2 );
Printf ("pos = % d \ n", pos );


// Second search
Pos = BFIndex (myString1, pos + 1, myString2 );
Printf ("pos = % d \ n", pos );
Return 0;

}


The brute-force algorithm is simple and easy to understand. In most cases, the algorithm is more efficient, but in some cases, the time efficiency of the brute-force algorithm is not high, the main reason is that when the master string and the Child string already have more than one character that is equal, as long as one character is not equal, the comparison position of the master string needs to be rolled back.













Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.