#include < stdio.h >
main ( void )
{
int c;
int number;
number = 0; //initialization space numbers
while ( ( c = getchar() ) != EOF )
{
if ( c == ' ' )
{
++number;
if ( number == 1 )
putchar( c );
}
if ( c != ' ' )
{
putchar( c );
number = 0;
}
}
return 0;
}
第三种:
使用一个while循环来“承接”多个空格
#include < stdio.h >
main ( void )
{
int c;
while ( ( c = getchar() ) != EOF )
{
if ( ' ' == c )
{
putchar ( c );
while ( ( c = getchar() ) == ' ' && c != EOF )
;
}
if ( EOF == c )
break;
putchar ( c );
}
return 0;
}
第四种:
使用一个字符pc作为“当前字符”之前的字符,通过pc来判断其后的空格是否输出
#include <stdio.h>
int main()
{
int c, pc;
pc = EOF;
while ((c = getchar()) != EOF) {
if (c == ' ')
if (pc != ' ')
putchar(c);
if (c != ' ')
putchar(c);
pc = c;
}
return 0;
}