急需!高手帮帮忙!用gets语句读入任意一个字符串(不超过30个字符),删除掉其中的连续空格.

来源:百度知道 编辑:UC知道 时间:2024/05/16 10:16:38
如:若读入字符串"aaa bbb ccc",输出后的结果为"aaa bbb ccc".

#include <stdio.h>

void fun( char *s )
{
int i = 0, j;
while ( s[ i ] != '\0' )
{
if ( s[ i ] == ' ' && s[ i + 1 ] == ' ' )
{
j = i + 1;
while ( s[ j ] != '\0' )
{
s[ j ] = s[ j + 1 ];
j++;
}
i--;
}
i++;
}
}

int main( )
{
char data[ 30 ];
gets( data );
fun( data );
printf("%s\n", data);
return 0;
}