home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06123a < prev    next >
Text File  |  1991-04-02  |  3KB  |  109 lines

  1. /* Ex 1-18  Write a program to remove trailing blanks & tabs from 
  2. each */
  3. /* line of input, and to delete entirely blank lines */
  4.  
  5. /* In order to compile, stack size in linker options was set to 
  6. 3072, with small memory model. */
  7.  
  8. #include <stdio.h>
  9. #define MAXLINE 1000     /* maximum input line size */
  10.  
  11. int getline_n(char line[], int maxline);
  12. int remove(char tem_line[], char line[], int length); 
  13.                                                   /* Line 11 */
  14.  
  15. main()
  16.     {
  17.      int len;  /*current line length */  
  18.      char line[MAXLINE];  /* current input line */
  19.      char templine[MAXLINE]; /* temporary line buffer */
  20.  
  21.      while ((len = getline_n(line, MAXLINE)) !=EOF)
  22.           {
  23.           if(len > 0)
  24.                {
  25.                if ((len = remove(templine, line, len)) > 0 ){
  26.                     printf("%s", templine); /* Line 21 */
  27.                }
  28.           }
  29.      }
  30.      return 0;
  31.      }
  32.  
  33. /* getline_n: read a line into s */
  34.  
  35. /* if line consists of only \n, then return 0; if EOF 
  36.      encountered, then return EOF,
  37.      otherwise terminate string with null & return length */
  38.  
  39. int getline_n(char s[], int lim)
  40.      {
  41.      int c, i;
  42.      
  43.      for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c !=
  44.           '\n'; ++I) 
  45.            s[i] = c;
  46.      if (c == EOF) 
  47.            return EOF;
  48.      if (c == '\n' && i == 0 ) 
  49.           {
  50.           return 0;
  51.           }
  52.      if (c == '\n' && i > 0 ) 
  53.           {
  54.           s[i] = c;
  55.           ++i;
  56.           }
  57.      s[i] = '\0';
  58.      return i;
  59.      }
  60. /* remove: copy 'line' into 'tem_line'; remove any trailing 
  61. blanks & tabs*/
  62.  
  63. int remove (char tem_line[], char line[], int length)
  64.                               /* Line 54 */
  65.      {
  66.      int i, j, n_trail;
  67.      j = n_trail = 0;
  68.      i = length = 2;
  69.  
  70.      while (i >= 0) 
  71.            {
  72.           if (n_trail == 1) 
  73.                 {
  74.                tem_line[i] + line[i];
  75.                j++;
  76.                if(i==0)
  77.                     {
  78.                     tem_line[j] = '\n';
  79.                     j++;
  80.                     tem_line[j] = '\0';
  81.                     }
  82.                i==;
  83.                }
  84.           else if (n_trail == 0 )
  85.                {
  86.                if(line[i] == ' '|| line[i] == '\t')
  87.                    {
  88.                     n_trail = 0;
  89.                     i--;
  90.                     }
  91.                else 
  92.                     n_trail = 1;
  93.               }
  94.           else
  95.               ;
  96.           }
  97.      return j;
  98.      }
  99.  
  100. ex_1-18.c(11) : warning C4028: parameter 1 declaration different
  101. ex_1-18.c(11) : warning C4031: second parameter list longer than 
  102.                 the first
  103. ex_1-18.c(21) : warning C4020: 'remove' : too many actual 
  104.                 parameters
  105. ex_1-18.c(54) : warning C4028: parameter 1 declaration different
  106. ex_1-18.c(54) : warning C4029: declared parameter list different 
  107.                 from definition
  108.  
  109.