Write a program to remove trailing blanks and tabs from each line of input,and to delete entirely blank lines.
1、各种常用键的ASCII码值是多少,参见网页:查看常用键的ASCII码值。
空格:32
制表:9
………………………………………………………………………………………………………………………………………………………
#include<stdio.h>
#define MAXLINE 1000
int getlines(char line[],int maxline);
int removes(char s[]);
/*print the new input line*/
main()
{
char line[MAXLINE];
while(getlines(line,MAXLINE)>0)
if(removes(line)>0)
printf("%s",line);
return 0;
}
int getlines(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';
return i;
}
int removes(char s[])
{
int i;
i = 0;
while(s[i] != '\n')
++ i;
i = i-1;
while(i >= 0 && (s[i] == ' '||s[i] == '\t'))
--i;
if(i>=0){
++ i;
s[i] = '\n';
++ i;
s[i] = '\0';
}
return i;
}
delete.c
这一题目当时理解有误,以为是删除单词之间的多余空格和tab键,后来才明白题目要求很简单,trailing blanks是指输入结束后,文本末尾多余的空格和tab键。
NOTE:绝对不能自以为是,或许有些时候就是简单的理解偏差而已。

830

被折叠的 条评论
为什么被折叠?



