您的位置:首页 > 编程语言 > C语言/C++

c语言:编写一个将输入复制到输出的程序,并将其中的多个空格用一个空格代替

2011-10-28 17:09 956 查看
《c语言程序设计 第二版》上的题目

1.编写一个将输入复制到输出的程序,并将其中的多个空格用一个空格代替?

直接看代码:

仅供参考,代码来源于互联网!!!

代码一:

#include "stdio.h"

main()
{
int c;
int i;
int n = 0;

while ( (c = getchar()) != EOF)
{
if ( c != '' )
{
putchar(c);
}
else if ( n != '')
{
putchar(c);
}

n = c;
}
}


代码二(详细):

#include <stdio.h>

/* count lines in input */
int
main()
{
int c, pc; /* c = character, pc = previous character */

/* set pc to a value that wouldn't match any character, in case
this program is ever modified to get rid of multiples of other
characters */

pc = EOF;

while ((c = getchar()) != EOF) {
if (c == '')
if (pc != '')   /* or if (pc != c) */
putchar(c);

/* We haven't met 'else' yet, so we have to be a little clumsy */
if (c != '')
putchar(c);
pc = c;
}

return 0;
}


PS:偶看了真是泪流满面,泪奔~~o(>_<)o ~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐