[C] string processing-Splits a string using a specified string (Chinese characters are supported)
Function stringsplit (Splits a string into a string array, where the array contains 0th digits as the number of split strings)
Stringsplit_struct (implement this function by defining a new structure)
/* C code is as follows */
# Include <stdio. h>
/* Solution 1 */
/* Split the string into a string array. The first part of the array is the number of splits */
Char ** stringsplit (const char * string, const char * Split)
{
Char ** result;
/* First allocate a char * memory, and then dynamically allocate the remaining memory */
Result = (char **) malloc (sizeof (char *) * 1 );
Memset (result, 0, sizeof (char *) * 1 );
/* Define a pointer for traversal and a pointer for location search */
Char * P = string;
Char * Pos = string;
/* No matter whether the split string exists or not, it is definitely split into a string */
Int COUNT = 1;
While (* P! = '\ 0 ')
{
Char * temp;
Char * tt;
/* Search for this string */
Pos = strstr (p, split );
/* If the result is 0, the remaining strings do not contain this character */
If (Pos = 0)
{
Result = (char **) realloc (result, sizeof (char *) * (count + 2 ));
Result [0] = count;
Result [count] = P;
Result [count + 1] = NULL;
Return result;
}
/* Allocate temporary string space */
Temp = (char *) malloc (sizeof (char) * (POS-p + 1 ));
Memset (temp, 0, sizeof (char) * (POS-p + 1 ));
/* Set the header pointer for use when assigning values */
Tt = temp;
While (P <= POS)
{
* Temp ++ = * P ++;
}
/* Set the end of the string to zero */
* -- Temp = '\ 0 ';
Result = (char **) realloc (result, sizeof (char *) * (count + 1 ));
Result [0] = count;
Result [count] = tt;
Count ++;
/* Set the pointer of the next time (important ). When the split length is greater than 1, if this parameter is not set, additional values are assigned to unnecessary strings */
P + = strlen (split)-1;
}
Return result;
}
/* Implementation solution 2 */
/* For the structure defined for convenient counting, the string array is assigned a value from 0 */
Typedef struct {
Int number;/* Number of split strings */
Char ** string;/* string array */
} Stringtab;
/* Split the string into a string array */
Stringtab stringsplit_struct (const char * string, const char * Split)
{
Stringtab result;
/* First allocate a char * memory, and then dynamically allocate the remaining memory */
Result. String = (char **) malloc (sizeof (char *) * 1 );
Memset (result. String, 0, sizeof (char *) * 1 );
/* No matter whether the split string exists or not, it is definitely split into a string */
Result. Number = 0;
/* Define a pointer for traversal and a pointer for location search */
Char * P = string;
Char * Pos = string;
While (* P! = '\ 0 ')
{
Char * temp;
Char * tt;
/* Search for this string */
Pos = strstr (p, split );
/* If the result is 0, the remaining strings do not contain this character */
If (Pos = 0)
{
Result. String = (char **) realloc (result. String, sizeof (char *) * (result. Number + 1 ));
Result. String [result. Number] = P;
Return result;
}
/* Allocate temporary string space */
Temp = (char *) malloc (sizeof (char) * (POS-p + 1 ));
Memset (temp, 0, sizeof (char) * (POS-p + 1 ));
/* Set the header pointer for use when assigning values */
Tt = temp;
While (P <= POS)
{
* Temp ++ = * P ++;
}
/* Set the end of the string to zero */
* -- Temp = '\ 0 ';
Result. String = (char **) realloc (result. String, sizeof (char *) * (result. Number + 1 ));
Result. String [result. Number] = tt;
/* Add a counter */
Result. Number ++;
/* Set the pointer of the next time (important ). When the split length is greater than 1, if this parameter is not set, additional values are assigned to unnecessary strings */
P + = strlen (split)-1;
}
Return result;
}
Int main ()
{
/* Perform the test */
/* Test solution 1 */
Char ** array;
Array = stringsplit ("A/AAA // ha AA", "AAA ");
Int I;
For (I = 1; I <= (INT) array [0]; I ++)
{
Printf ("Num: % d I: % d: Value: % s \ n", array [0], I, array [I]);
}
Array = stringsplit ("A/aa ha a // ha AA", "ha ");
For (I = 1; I <= (INT) array [0]; I ++)
{
Printf ("Num: % d I: % d: Value: % s \ n", array [0], I, array [I]);
}
/* Solution 2 test */
Stringtab array2;
Array2 = stringsplit_struct ("A/AAA // ha AA", "AAA ");
For (I = 0; I <= array2.number; I ++)
{
Printf ("Num: % d I: % d: Value: % s \ n", array2.number, I, array2.string [I]);
}
Array2 = stringsplit_struct ("A/aa ha a // ha AA", "ha ");
For (I = 0; I <= array2.number; I ++)
{
Printf ("Num: % d I: % d: Value: % s \ n", array2.number, I, array2.string [I]);
}
Return 0;
}
Http://hi.baidu.com/nivrrex/item/8f0c8f175a9ce5721009b5a1