c语言的问题,急!!!!!

1、写一个函数去掉字符数组中第一个字母以前的所有空格,并且将字母中间的多个空格变成一个空格,然后在主函数中调用这个函数。
如字符串:_ _ _ _ _ abc_ _ _ _de_ _ _ _fg_ _ _ _
调用函数后字符串变为:abc_de_fg

(注:为便于理解把例子中的下划线认为是空格)

你的程序大致如下:

#include <stdio.h>

void compress_space(char *p1,char *p2);

main()
{
char a[30]={" abc de fg "};
char b[30];
compress_space(a,b);
printf("%s\n",b);
}

void compress_space(char *p1,char *p2)
{

..............

}

楼上的程序是错的,不满足第二个要求:“将字母中间的多个空格变成一个空格”

#include <stdio.h>

void compress_space(char *p1,char *p2);

main()
{
char a[30]={" abc de fg \0"};
char b[30];
compress_space(a,b);
printf("%s\n",b);
}

void compress_space(char *p1, char *p2)
{
for (; *p1 == ' '; p1++ ) ;
for (; *p2 = *p1; p2++ ) {
if ( *p2 == ' ' )
while( *++p1 == ' ' ) ;
else
p1++;
}
}
温馨提示:答案为网友推荐,仅供参考