Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. /*
  2.  *  getline.c - Count characters per-line from stdin. Strips white
  3.  *              space from end of line, and ignores 0-length lines.
  4.  *
  5.  *  Author: Ryan R. Uber <ryan@blankbmx.com>
  6.  *  Date:   31 Oct 2010 04:28:47PM CDT
  7.  *
  8.  *  Modified from section 1.9 (Character Arrays)
  9.  *  "The C Programming Language" by Brian Kerrigan and Dennis Ritchie
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #define MAXLINE 1000
  15.  
  16. /*  Here we initialize getline(). I am unclear on why it is initialized
  17.  *  at the top of the file with different arguments than the actual
  18.  *  function. It works identically without this initialization.
  19.  */
  20. int getline(char line[], int maxline);
  21.  
  22. main()
  23. {
  24.     int len;
  25.     char line[MAXLINE];
  26.  
  27.     /*  Gather lines that have a non-zero length. This would include *any*
  28.      *  line in a file, even a blank line, as that would have a length of
  29.      *  1 for the '\n' character.
  30.      */
  31.     while ((len = getline(line, MAXLINE)) > 0 )
  32.     {
  33.         /*  As stated previously, a blank line has a length of 1. Therefore,
  34.          *  in order to skip empty lines, we only want to look at lines with
  35.          *  length of 2 or greater.
  36.          */
  37.         if ( len > 1 )
  38.             printf("%6d %s", len-1, line);
  39.     }
  40.  
  41.     return 0;
  42. }
  43.  
  44. int getline(char s[], int lim)
  45. {
  46.     int c, i, n;
  47.  
  48.     for ( i=0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i )
  49.         s[i] = c;
  50.  
  51.     if ( c == '\n' )
  52.     {
  53.         /*  This part of the program strips the white space from the end of
  54.          *  each line. This is tricky because we cannot do this until after
  55.          *  the entire line of input is read, including the white space. Thus,
  56.          *  we have to remove the white space characters from the s[] array,
  57.          *  and decrement any counters we are using to compensate for the
  58.          *  loss of characters. Here we loop backwards through the s[] array
  59.          *  once a newline is encountered.
  60.          */
  61.         for ( n = i-1; n >= 0; n-- )
  62.         {
  63.             /*  The variable "n" is the previous element's offset in the array.
  64.              *  Since we found a newline character, let's see if the character
  65.              *  before it was white space.
  66.              */
  67.             if ( s[n] == ' ' || s[n] == '\t' )
  68.             {
  69.                 /*  Here we move zero the white space element in the array and
  70.                  *  decrement the character count variable "i".
  71.                  */
  72.                 s[n] = '\0';
  73.                 i--;
  74.             }
  75.  
  76.             /*  If there was no white space at the end of the string, add the
  77.              *  newline, increment "i", and break out of the backward loop.
  78.              */
  79.             else
  80.             {
  81.                 s[i] = c;
  82.                 ++i;
  83.                 break;
  84.             }
  85.         }
  86.  
  87.         /*  My ghetto way of fixing the 0-return on lines containing *only* '\n'.
  88.          *  This entire function needs work so that I can remove this terribleness.
  89.          */
  90.         if ( i < 1 )
  91.             i = 1;
  92.     }
  93.  
  94.     s[i] = '\0';
  95.  
  96.     return i;
  97. }
  98.  
  99. /* EOF */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement