c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
编写这样一个程序,实现将输入流复制到输出流,但是要将输入流中多个空格过滤成一个空格。
1.旗帜变量方法
#include <stdio.h> int main(void)
{
int c;
int inspace;
//这里用了旗帜变量来过滤多余空格
inspace = ;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(inspace == )
{
inspace = ;
putchar(c);
}
} /* We haven't met 'else' yet, so we have to be a little clumsy */
if(c != ' ')
{
inspace = ;
putchar(c);
}
} return ;
}
2.保存上一个输入字符
Chris Sidi writes: "instead of having an "inspace" boolean, you can keep track of the previous character and see if both the current character and previous character are spaces:"
Chris Sidi 写道:“我们可以不用‘inspace’这样一个布尔型旗帜变量,通过跟踪判断上一个接收字符是否为空格来进行过滤。”
#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 ;
}
3.利用循环进行过滤
Stig writes: "I am hiding behind the fact that break is mentioned in the introduction"!
#include <stdio.h> int main(void)
{
int c;
while ((c = getchar()) != EOF) {
if (c == ' ') {
putchar(c);
while((c = getchar()) == ' ' && c != EOF)
;
}
if (c == EOF)
break; /* the break keyword is mentioned
* in the introduction...
* */ putchar(c);
}
return ;
}
c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格的更多相关文章
- c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
Revise the main routine of the longest-line program so it will correctly print the length of arbitra ...
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...
- c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...
- c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()
The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...
- c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。
Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...
- c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...
- C程序设计语言(第二版)习题:第一章
第一章虽然感觉不像是个习题.但是我还是认真去做,去想,仅此而已! 练习 1-1 Run the "hello, world" program on your system. Exp ...
随机推荐
- Codevs 1218 疫情控制 2012年NOIP全国联赛提高组
1218 疫情控制 2012年NOIP全国联赛提高组 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description H 国有 n 个城市,这 ...
- IE兼容性问题
1.H5标签兼容.解决:js:document.createElement("footer");css:display: block;或者直接使用 html5shiv.js ...
- C#基础(三)—重载与覆盖
所谓重载指的是同一个类中有两个或多个名字相同但是参数不同的方法.重载,必然发生在一个类中,函数名相同,参数类型或者顺序不同构成重载,与返回类型无关. override:过载也称重写是指子类对父类中虚函 ...
- android hander 线程用法
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...
- aspx页面状态管理Cookie和ViewState
Cookie 设置cookie protected void Button2_Click(object sender, EventArgs e) { HttpCookie cookie = new H ...
- 我们都忽略了Html5的力量,如果只看成一种技术就大错特错了!
第一部分:Html5市场的力量 我们太忽略Html5的市场力量了.如果你把Html5当作一种技术,就大错特错了!忘掉你的产品,忘掉你的技术,想想移动时代的信息传播和消费场景.作为2B,我们实在是没有重 ...
- linux点滴:NFS
介绍 NFS,Network File System,网络文件系统.主要功能是通过网络让不同的主机系统间共享资源,类似于windows下的文件共享.适用于互联网中小型企业. 工作原理 客户端发送请求 ...
- 【原创】一起学C++ 之 字符串 ---------C++ primer plus(第6版)
C++ Primer Plus 第6版 字符串:是存储在内存的连续字节中的一系列字符. C++处理字符串的方式有2种: 一.来自C语言.常被称为C-风格字符串(C-Style-string) 1)从字 ...
- MaskedTextBox控件实现输入验证
Mask属性可以验证用户在文本中输入数据的格式 this.maskedTextBox1.Mask = "000000-00000000-000A";//身份证号码18位 this. ...
- 当页面编辑或运行提交时,出现“从客户端中检测到有潜在危险的request.form值”问题,该怎么办呢?
最近在学习highcharts时,关于其中的导出功能,本来是想把导出的图片存放在本地,发现只有在电脑联网的情况下才可以一下导出图片,后来查阅了一番资料,才发现highcharts中的导出默认的官网服务 ...