C复习笔记(7)-7.4
来源:互联网 发布:编程教科书 编辑:程序博客网 时间:2024/05/17 23:24
My method, it seems easy, because I read a line a time!
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entirely blank lines.
#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int getline(char line[], int maxline);
int trail_line(char line[],int length);
int main(void)
{
int len; /* current line length */
char line[MAXLINE]; /* current input line */
while ((len = getline(line,MAXLINE)) > 0)/*actuall length or the line,contain '/n'*/
{
printf("%s:%d/n",line,len);
/*At least more than a '/n' exit!*/
if(len > 1)
len = trail_line(line,len);
/*At least more than a '/n' exit after trailing!*/
if(len > 1)
printf("%s:%d",line,len);
}
return 0;
}
/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
int c, i;
for (i = 0; i < lim-1 && (c=getchar())!= EOF && c != '/n'; ++i)
s[i] = c;
if (c == '/n')
{
s[i] = c;
++i;
}
s[i] = '/0';// Notice it is needed!
return i;
}
int trail_line(char line[], int length)
{
int i;
i = length-2;
while(i >= 0)
{
if(line[i] == ' ' || line[i]== '/t')
i--;
else
{
line[++i] = '/n';
line[++i] = '/0';
break;
}
}
return i;
}
Method 2 in C Answer Book
#include <stdio.h>
#include <stdlib.h>
#define MAXQUEUE 1001
int advance(int pointer)
{
if (pointer < MAXQUEUE - 1)
return pointer + 1;
else
return 0;
}
int main(void)
{
char blank[MAXQUEUE];/*it is used to record the blank chars, is a queue*/
int head, tail;
int nonspace;
int retval;
int c;
retval = nonspace = head = tail = 0;
/*Read a char a time*/
while ((c = getchar()) != EOF)
{
if (c == '/n')
{
head = tail = 0;
if (nonspace)/*not an entirely blank line*/
putchar('/n');
nonspace = 0;
}
else if (c == ' ' || c == '/t')
{
if (advance(head) == tail) /*whether the consecutively occured blank chars overflow*/
{
putchar(blank[tail]);
tail = advance(tail);
nonspace = 1;
retval = EXIT_FAILURE;
}
blank[head] = c;
head = advance(head);
}
else
{
while (head != tail)
{
putchar(blank[tail]);/*print blank value first*/
tail = advance(tail);
}
putchar(c);/*print the non-blank char secondly*/
nonspace = 1;
}
}
return retval;
}
Method3: fix a bug in method 2
if there is a huge block of non-trailing whitespace (eg "A",2000
spaces, "B/n") method1 returns an error when there's not a need for
it.
#include <stdio.h>
#include <stdlib.h>
#define MAXQUEUE 1001
int advance(int pointer)
{
if (pointer < MAXQUEUE - 1)
return pointer + 1;
else
return 0;
}
int main(void)
{
char blank[MAXQUEUE];
int head, tail;
int nonspace;
int retval;
int c;
int spaceJustPrinted; /*boolean: was the last character printed whitespace?*/
retval = spaceJustPrinted = nonspace = head = tail = 0;
while ((c = getchar()) != EOF) {
if (c == '/n') {
head = tail = 0;
if (spaceJustPrinted == 1) /*if some trailing whitespace was printed...*/
retval = EXIT_FAILURE;
if (nonspace) {
putchar('/n');
spaceJustPrinted = 0; /* this instruction isn't really necessary since
spaceJustPrinted is only used to determine the
return value, but we'll keep this boolean
truthful */
nonspace = 0; /* moved inside conditional just to save a needless
assignment */
}
}
else if (c == ' ' || c == '/t') {
if (advance(head) == tail) {
putchar(blank[tail]); /* these whitespace chars being printed early
are only a problem if they are trailing,
which we'll check when we hit a /n or EOF */
spaceJustPrinted = 1;
tail = advance(tail);
nonspace = 1;
}
blank[head] = c;
head = advance(head);
}
else {
while (head != tail) {
putchar(blank[tail]);
tail = advance(tail);
}
putchar(c);
spaceJustPrinted = 0;
nonspace = 1;
}
}
/* if the last line wasn't ended with a newline before the EOF,
we'll need to figure out if trailing space was printed here */
if (spaceJustPrinted == 1) /*if some trailing whitespace was printed...*/
retval = EXIT_FAILURE;
return retval;
}
- C复习笔记(7)-7.4
- C复习笔记(7)-7.4
- C语言复习笔记 7
- c复习笔记
- C语言复习笔记
- C语言复习笔记
- C笔记复习
- C--复习笔记
- C & C++ 复习笔记
- C复习笔记(1)-6.16
- C复习笔记(2)-6.18
- C复习笔记(3)-6.19
- C复习笔记(4)-6.20
- C复习笔记(5)-6.23
- C复习笔记(6)-6.24
- c语言复习笔记1
- C语言老师复习笔记
- C语言复习笔记 1
- 移除程序的好帮手-Windows Installer CleanUp
- request.getParameter("变量名")获取的字符串中原来的加号变成了空格,url传值
- 论维护期
- Excel导入数据库
- C复习笔记(7)-7.4
- C复习笔记(7)-7.4
- 编写简练代码是程序员的职业修养之本
- 国内几个实用的.NET技术网站
- MS SQL 2005 无法启动,错误代码5118
- 趣谈“去睡觉”
- Visual Studio 中的实时运算窗口
- 开始学习C#
- [英语阅读]美变性男子顺利产下女婴
- Lutz Roeder's .NET Reflector的使用方式