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 ...
随机推荐
- config spec
config_spec Rules for selecting versions of elements to appear in a view APPLICABILITY Product Comma ...
- php header示例代码(推荐)
<?php /*** Function: PHP header() examples (PHP) ** Desc: Some examples on how to use the header( ...
- JS到PHP使用RSA算法进行加密通讯
我们平时做用户登录表单提交,用户名密码都是明文直接POST到后端,这样很容易被别人从监听到. 在js上做rsa,感觉jsencrypt这个是封装的比较好的,但用起来还是遇到了些坑,所以踩进代码里填填坑 ...
- Javascript 计算分页
var getPageData = function (last_page, current_page) { var numberLinks = []; var i = 0; for (i; i &l ...
- Python之路----文件操作
文件操作 1.能调用方法的一定是对象,比如数值.字符串.列表.元组.字典,甚至文件也是对象,Python中一切皆为对象. str1 = 'hello' str2 = 'world' str3 = ' ...
- Windows下使用cmd启动Oracle EM和sql命令使用+主机身份认证
(1)cmd命令下使用sql命令 >sqlplus / as sysdba sql>select * from v$version; (2)cmd命令下启动Oracle EM 安装完ora ...
- wordpress mobile templates
http://themeforest.net/category/wordpress/mobile http://themeforest.net/item/monolith-wp-theme-for-b ...
- <六> jQuery 获得内容和属性
获得内容 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - ...
- ios短信和电话--参考
调用打电话功能 [[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]]; 调 ...
- jQuery滑动导航菜单
js: $(function(){ $("ul.sub").parent().append("<span></span>"); $(&q ...