C复习笔记(7)-7.4

 

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;
   
   
} 
   
   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值