1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5
6 char* mytrim(char* sDes, const char* sSrc)
7 {
8 if(sSrc == NULL || sDes == NULL)
9 return NULL;
10 char* tmp = sDes;
11 while(*sSrc == ' '||*sSrc == '\t' ||*sSrc== '\r'||*sSrc== '\n')
12 {
13 sSrc++;
14 }
15 while(*sSrc!= 0x00)
16 {
17 *sDes = *sSrc;
18 sDes++;
19 sSrc++;
20 }
21 sDes--;//should do -- because current it point to nothing
22 while(*sDes == ' ' ||*sDes == '\t' ||*sDes== '\r'||*sDes== '\n')
23 {
24 sDes--;
25 }
26 sDes++;//should do ++ because current it point to the last character
27 *sDes = 0x00;//ending character
28 printf("tmp is%sending\n",tmp);
29 return tmp;
30 }
31 int main()
32 {
33 const char* sSrc = " abc def ";
34 size_t len = strlen(sSrc);
35
36 char* sDes = (char*) malloc(len*sizeof(char)+1);
37 sDes = mytrim(sDes,sSrc);
38 free(sDes);
39
40 }
trim 函数c实现
最新推荐文章于 2021-09-27 09:04:08 发布