Removing the plurality of strings spaces, one space

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 main()
 5 {
 6     char *str = "123   abc   456   def   7";
 7     char buf[32];
 8     int j = 0, i = 0;
 9 
10     memset(buf, '\0', sizeof(buf));
11     for (i = 0; i < strlen(str); ++i) {
12         if (str[i] == ' ') {
13             buf[j] = ' ';
14             for (i; i < strlen(str); ++i) {
15                 if (str[i] != ' ') {
16                     ++j;
17                     buf[j] = str[i];
18                     break;
19                 }   
20             }   
21         } else {
22             buf[j] = str[i];
23         }   
24         ++j;
25     }   
26     printf("buf : %s\n", buf);
27 }

 

Guess you like

Origin www.cnblogs.com/coolYuan/p/11390423.html