lolamontes69

K+R Exercise1_18

Sep 5th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAXLINE 1000
  4.  
  5. int getline(char line[], int maxline);
  6.  
  7. int main()
  8. {
  9.     int len;
  10.     char line[MAXLINE];
  11.  
  12.     while((len = getline(line, MAXLINE)) > 0)
  13.         if(len > 1)                          // len(1) is a '\n'
  14.             printf("%d, %s\n",len, line);    // len left in to prove it was shortened.
  15.     return(0);
  16. }
  17.  
  18. int getline(char s[], int lim)
  19. {
  20.     int c, i;
  21.  
  22.     for(i=0; i<lim-2 && (c=getchar())!=EOF && c!='\n'; ++i)
  23.         s[i]=c;
  24.     if(c=='\n') {
  25.         while(s[i-1]==' ' || s[i-1]=='\t')      // Note chars in '' not ""
  26.             --i;
  27.         s[i]=c;
  28.         ++i;
  29.     }
  30.     s[i]='\0';
  31.     return i;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment