lolamontes69

K+R Exercise1_22

Sep 5th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define ACL 30
  4. #define IN 1
  5. #define OUT 0
  6. #define TAB 8
  7.  
  8. int main()
  9. {
  10.     int c, i, j, d, lc;           // d=wordcount, lc=letter count
  11.  
  12.     char word[ACL];
  13.  
  14.     d=0;
  15.     lc=0;                         // Always initialize numeric variables
  16.     while((c=getchar())!=EOF) {
  17.  
  18.         if((lc>=ACL && c==' ') || (lc>=ACL && c=='\n')) {
  19.             printf("(Exceeded Allowed Max-word-length %d)\n", ACL);
  20.             lc=0;
  21.             d=0;
  22.         }
  23.  
  24.         if(d > ACL) {             // puts a new line when ACL reached
  25.             printf("\n");         // if still in word the word keeps getting
  26.             d=0;                  // added to word
  27.         }
  28.  
  29.         if(c==' ' || c=='\n') {
  30.             for(i=0; i<lc; ++i)
  31.                 printf("%c",word[i]);
  32.             if(d<ACL)
  33.                 putchar(c);
  34.             lc=0;
  35.         }
  36.  
  37.         else if(c=='\t') {
  38.             for(i=0; i<lc; ++i)
  39.                 printf("%c",word[i]);
  40.             lc=0;
  41.             if(d+(d%TAB)>ACL) {
  42.                 printf("\n");
  43.                 d=0;
  44.             }
  45.             else {
  46.                 for(j=0; j<=d%TAB; ++j) {
  47.                     printf(" ");
  48.                     ++d;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         else {
  54.             word[lc]=c;
  55.             ++lc;
  56.         }
  57.  
  58.         if(c=='\n') d=0;          // reset d after newline
  59.         else if(c=='\t')          // d already calculated
  60.             ;
  61.         else ++d;
  62.     }
  63.     printf("\nlc=%d",lc);
  64.     return(0);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment