字符数组拆分
执行结果
1 /* 2 * @Author: wujianfei 3 * @Date: 2018-04-10 21:09:59 4 * @Last Modified by: wujianfei 5 * @Last Modified time: 2018-04-12 21:08:57 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 typedef struct { 11 int dime_1_count; 12 }KeyVal; 13 14 char** DimeRoom = NULL; 15 int dime_2_count_store_[100]; 16 17 KeyVal getKeyValueCharArray(const char *chars,const int len,char splitChar){ 18 19 //========计算第一维度的长度dime_1_count+1========================================== 20 int dime_1_count = 0;//存储键值对type=value的数量用于创建二维数组的第一维度 21 int dime_2_count = 80;//定义第二维度存储的字符数组长度(type=value拆成数组的长度) 22 int index0 = 0; 23 while(index0<len){ 24 if(chars[index0] == splitChar){ 25 dime_1_count++; 26 } 27 index0++; 28 } 29 dime_1_count++; 30 //==========计算第二维度的长度======================================= 31 int dime_2_count_store[dime_1_count];//存储每个二维的长度 32 int index1 = 0;//标记chars 33 int tempLengthStore = 0;//临时存储,存储每次计算的第二维度的长度 34 int current_dime_1 = 0;//存放当前所在的维度 35 int lastIndex0 = 0;//最后一个逗号的位置 36 while(index1<len){ 37 if(chars[index1] == splitChar){ 38 dime_2_count_store[current_dime_1] = tempLengthStore;//把第二维度的长度存储到第一维度中 39 printf("> %d \n",index1); 40 tempLengthStore = 0;//初始化当前维度 41 current_dime_1++;//计算下一个维度 42 lastIndex0 = index1;//标记最后一个逗号的位置 43 }else{ 44 tempLengthStore++; 45 } 46 index1++; 47 } 48 //最后一段长度 49 tempLengthStore = 0; 50 lastIndex0++; 51 while(lastIndex0<len){ 52 tempLengthStore++; 53 lastIndex0++; 54 } 55 dime_2_count_store[dime_1_count-1] = tempLengthStore; 56 //==========往store中存储字符================================================================== 57 int dimeIndex_1 = 0;//一维 58 int dimeIndex_2 = 0;//二维 59 int lastIndex1 = 0; //最后一次出现逗号的位置 60 char tempStore[80];//存key=value的临时数组 61 int tempStoreIndex = 0;//临时存储游标 62 int index3 = 0; 63 while(index3<sizeof(tempStore)){ 64 tempStore[index3] = 0; 65 index3++; 66 } 67 char store[dime_1_count][80];//用二维来存储键值 68 //将store上的所有位清0 69 int storeClearIndex = 0; 70 while(storeClearIndex<dime_1_count){ 71 int storeClearIndex_2dime = 0; 72 while(storeClearIndex_2dime<80){ 73 store[storeClearIndex][storeClearIndex_2dime] = 0; 74 storeClearIndex_2dime++; 75 } 76 storeClearIndex++; 77 } 78 79 int storeCurrentRow = 0;//用来标记存储到第几行了 80 int index2 = 0; //用来循环遍历的 81 while(index2<len){ 82 if(chars[index2] == splitChar){ 83 dimeIndex_1++;//一维移位 84 lastIndex1 = index2;//记录最后出现逗号的位置 85 int index87 = 0; 86 while(index87<sizeof(tempStore)){ 87 store[storeCurrentRow][index87] = tempStore[index87]; 88 index87++; 89 } 90 storeCurrentRow++; 91 int thisTemp = 0; 92 while(thisTemp<sizeof(tempStore)){ 93 tempStore[thisTemp] = 0; 94 thisTemp++; 95 } 96 tempStoreIndex = 0; 97 }else{ 98 tempStore[tempStoreIndex] = chars[index2]; 99 tempStoreIndex++; 100 dimeIndex_2++;//二维移位 101 } 102 index2++; 103 } 104 105 //清理最后一行上的每个位 106 int index4 = 0; 107 while(index4<80){ 108 store[storeCurrentRow][index4] = 0; 109 index4++; 110 } 111 //存储最后一行 112 int index5 = 0; 113 while(index5<sizeof(tempStore)){ 114 store[storeCurrentRow][index5] = tempStore[index5]; 115 index5++; 116 } 117 // printf("> %s %s %s %s \n",store[0],store[1],store[2],store[3]); 118 //创建二级指针,用于存放二维数组,这里创建dime_1_count行 119 DimeRoom = (char *)malloc(sizeof(char *)*dime_1_count); 120 //将二维数组转为二级指针 121 int index6 = 0; 122 while(index6<dime_1_count){ 123 DimeRoom[index6] = store[index6]; 124 index6++; 125 } 126 //=========测试================================================================ 127 // int index77 = 0; 128 // while(index77<dime_1_count){ 129 // printf("%d \n",dime_2_count_store[index77]); 130 // index77++; 131 // } 132 133 // int index79 = 0; 134 // while(index79<dime_1_count){ 135 // printf(">>>>>>>>>>%s \n",*(dime+index79)); 136 // index79++; 137 // } 138 // printf("===================fuction is over====================================\n"); 139 KeyVal keyV; 140 keyV.dime_1_count = dime_1_count; 141 int index7 = 0; 142 while(index7<dime_1_count){ 143 printf("====>%d \n",dime_2_count_store[index7]); 144 dime_2_count_store_[index7] = dime_2_count_store[index7]; 145 index7++; 146 } 147 return keyV; 148 } 149 150 151 int main () { 152 printf("starting...\n"); 153 char chars[100] = {'t','y','p','e','=','v','a','l','u','e',',', 154 't','y','p','e','2','=','v','a','l','u','e','2',',', 155 't','y','p','e','3','=','v','a','l','u','e','3',',','t'}; 156 KeyVal keyV = getKeyValueCharArray(chars,38,','); 157 printf("keyV.dime_1_count = %d \n",keyV.dime_1_count ); 158 159 int i = 0; 160 while(i<keyV.dime_1_count){ 161 printf("main>>>>>>>>%s \n",*(DimeRoom+i)); 162 i++; 163 } 164 165 int i2 = 0; 166 while(i2<keyV.dime_1_count){ 167 printf("dime_2_count_store_>>>>>>>>%d \n",dime_2_count_store_[i2]); 168 i2++; 169 } 170 171 free(DimeRoom); 172 return 0; 173 }
执行结果:
。
以雷霆击碎黑暗