lolamontes69

K+R Exercise7_2

Sep 25th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. /* Write a program that will print arbitrary input in a sensible way. As a minimum,
  2.  * it should print non-graphic characters in octal or hexdecimal according to
  3.  * local custom, and break long text lines.
  4.  *
  5.  * Can haz commandline input.
  6.  * ./exercise7_2 < someText.txt
  7.  *
  8.  * */
  9.  
  10. #include <stdio.h>
  11.  
  12. #define MAXCHAR 80
  13. #define MAXTEMP 21  /* Maximum characters to store in temp */
  14.  
  15. void charprinter(int i, int j);
  16.  
  17. int cc;
  18.  
  19. main()  /* convert declaration to words */
  20. {
  21.     int c, d, st=0, i;
  22.     char temp[MAXTEMP];
  23.  
  24.     cc = 0;
  25.     while((c = getchar()) != EOF) {
  26.         if(cc<MAXCHAR-20) {
  27.             charprinter(c, 0);
  28.         }
  29.         else if(cc>=MAXCHAR-20 && c==' ' && st>0) {
  30.             for(i=0; i<st; i++) {
  31.                 d = temp[i];
  32.                 charprinter(d, 1);
  33.             }
  34.             putchar(c);
  35.             cc++;
  36.             st=0;
  37.         }
  38.         else {
  39.             temp[st++] = c;
  40.             cc++;
  41.             if(cc>=MAXCHAR) {
  42.                 printf("\n");
  43.                 cc=0;
  44.                 for(i=0; i<st; i++) {
  45.                     d = temp[i];
  46.                     charprinter(d, 1);
  47.                 }
  48.                 st=0;
  49.             }
  50.         }
  51.     }
  52.     puts(" ");
  53.     return 0;
  54. }
  55.  
  56. /* charprinter: to avoid repetition */
  57. void charprinter(int i, int j)
  58. {
  59.     if(i==10) {
  60.         if(!j) {
  61.             printf("\n"); /* print '\n' as normal */
  62.             cc=-1;
  63.         }
  64.         else if(j) {
  65.             printf(" ");
  66.             cc++;
  67.         }
  68.     }
  69.     else if(i<32) {
  70.         printf("[0x%x]",i);
  71.         cc+=6;
  72.     }
  73.     else {
  74.         putchar(i);
  75.         cc++;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment