当函数发现字符串中如果有一个地方由一个或多个连续的空格组成,就把它们改成单个空格字符。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 void deblank(char str[]);
 5 int main()
 6 {
 7     char str[100] ;
 8     memset(str,0,100);
 9     printf("please input the string str:");
10     gets(str);
11     deblank(str);
12     puts(str);
13     system("pause");
14     return 0;
15 }
16 void deblank(char str[])
17 {
18     int i = 0;
19     int j = 0;
20     while(str[i] != '\0'&& str[i+1] != '\0')
21     {
22         if(str[i] != ' ' || (str[i] == ' ' && str[i+1] != ' '))
23         {
24            str[j] = str[i];
25             j++;
26             i++;
27         }
28         else
29             {
30                 i++;
31         }
32     }
33     str[j+1] = '\0';
34 }

 

posted @ 2015-05-05 21:10  码农@163  阅读(430)  评论(0编辑  收藏  举报