1#include "stdio.h"
2#include "string.h"
3#include "stdlib.h"
4
5char* mergeblank(char str[]);
6
7int main()
8{
9 char str[]= "hello world this is a \"t e s t\"this is a test";
10 printf("str_old: %s\n", str);
11 char *p = mergeblank(str);
12 printf("str_new: %s\n", p);
13 system("pause");
14 return 0;
15}
16
17char* mergeblank(char str[])
18{
19 int iBlank = 0, iPos = 0;
20 int i = 0, j=0;
21 int bYH = 0;
22 for (i=0; str[i]!='\0'; i++)
23 {
24 if (str[i] == '"')
25 {
26 if (!bYH)
27 {
28 bYH = 1;
29 }
30 else
31 {
32 bYH = 0;
33 }
34 }
35
36 if (str[i] == ' ' && !bYH)
37 {
38 if (iBlank == 0)
39 {
40 iPos = i;
41 }
42 iBlank ++;
43 }
44 else
45 {
46 //merge blank
47 if (iBlank > 1)
48 {
49 for (j=iPos+1; str[j]!='\0'; j++)
50 {
51 str[j] = str[j+iBlank-1];
52 }
53
54 i = i-iBlank+1;
55 }
56
57 iBlank = 0;
58 }
59 }
60 return str;
61}