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 ...
随机推荐
- CruiseControl.NET : Configuration Preprocessor
Original link: http://build.sharpdevelop.net/ccnet/doc/CCNET/Configuration%20Preprocessor.html http: ...
- 九度OJ 1501 最大连续子序列乘积 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1501 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含 ...
- jquery ajax php 无刷新上传文件 带 遮罩 进度条 效果的哟
在很多项目中都会叫用户上传东西这些的,自从接触了jquery 和ajax之后就不管做什么,首先都会想到这个,我这个人呢?是比较重视客户体验的,这次我这边负责的是后台板块,然后就有一块是要求用户上传照片 ...
- 通过WebApi取出XML数据
Get请求: public static Result<GetExpressCollectionResponseType> GetDataFromWebs(string waybillNu ...
- C# 实体model验证输出
新建Model实体: [Required(ErrorMessage = @"地址 1 为必填项!")] [StringLength(, ErrorMessage = @" ...
- 大话F#和C#:是否会重蹈C#失败的覆辙?
F#.net 出来有些年头儿了,将从 VS 2010 起在 .net framework 平台上以“一等公民”身份粉墨登场的它,将会给计算机科技与软件工业带来哪些悲喜剧呢? F# 将扮演一个什么角色? ...
- rman全备份异机恢复
一.测试环境 [oracle@localhost ~]$ uname -a Linux localhost.localdomain -.el6.x86_64 # SMP Tue May :: EDT ...
- 查看 usb info
mount -t usbfs /proc/bus/usb /proc/bus/usb cat /proc/bus/usb/devices
- javascript debut trick, using the throw to make a interrupt(breakpoint) in your program
console.log('initialize'); try { throw "breakPoint"; } catch(err) {} when I debug the extj ...
- css中table-layout:fixed 属性的用法
table-layout:fixed 属性的用法:如果想要一个table固定大小,里面的文字强制换行(尤其是在一长串英文文本,并且中间无空格分隔的情况下),以达到使过长的文字 不撑破表格的目的,一般是 ...