Use C language to separate strings and C language to separate strings
# Include <stdio. h> int main () {char str1 [10] = {0}; char str2 [10] = {0}; char str3 [10] = {0 }; sscanf (", 3 #", "% [^ #] # % [^ #] # % [^ #]", str1, str2, str3 ); printf ("The split result is: \ n % s \ n", str1, str2, str3); return 0 ;} /* hovertree.com */
// [C Language] string processing-use a specified string to split a string (Chinese characters supported) //: The StringSplit function (Splits a string into a string array, where the array contains 0th bits as the number of split strings) //: function StringSplit_Struct (to implement this function by defining a new structure) /* C code is as follows */# include <stdio. h>/* Implementation Scheme 1 * // * He asked hovertree.com * // * to 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);/* defines a pointer for traversal and a pointer for finding a position */char * p = string; char * pos = string;/* No matter whether the split string exists or not, it is absolutely split into a string */int count = 1; while (* p! = '\ 0') {char * temp; char * tt;/* Find 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 to use */tt = temp when assigning values; 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 ). If the split length is greater than 1, an unnecessary string */p + = strlen (split)-1;} return result;} is assigned if this parameter is not set ;} /* Implementation Scheme 2 * // * He asked hovertree.com * // * 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, then dynamically allocate the remaining memory */result. string = (char **) malloc (sizeof (char *) * 1 ); Memset (result. string, 0, sizeof (char *) * 1);/* whether or not the split string exists, it is absolutely split into a string */result. number = 0;/* define a pointer for traversal and a pointer for searching for a position */char * p = string; char * pos = string; while (* p! = '\ 0') {char * temp; char * tt;/* Find this string */pos = strstr (p, split ); /* The result is 0, indicating that the remaining string does 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 to use */tt = temp when assigning values; 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;/* counter plus one */result. number ++;/* set the pointer of the next time (important ). When the split length is greater than 1, it will assign more values to unnecessary strings */p + = strlen (split)-1;} return result;} int main () {/* test * // * He asked hovertree.com * // * solution 1 test */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 ;}
Related: http://www.cnblogs.com/roucheng/p/cfenge.html