Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.

#include <stdio.h>
#define MAXLINE 1000       /*maximum input line size */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/*print longest input line */
int main(){
     int len;                               /*current line length */
     int max;                        /*maximum length seen so far */
    char line[MAXLINE];          /*longest line save here */

    max = 0;
    while((len = getline(line, MAXLINE)) > 0){
         printf("%d  , %s", len, line);
         if (len > max){
             max = len;
             copy(longest, line);
         }
    }
     if(max > 0)       /* there was a line */
        printf("%s", longest);
     return 0;
}
/*getline: read a line into s, return length    */
int getline(char s[], int lim)
{
     int c, i, j;
     j =0;
     for(i =0; (c =getchar()) != EOF && c != '\n'; ++i)
           if (i < lim -2){
               s[j] = c;       /* line still in boundaries */
               ++j;
           }
    if (c == '\n'){
         s[j] = c;
         ++j;
         ++i;
    }
     s[j] = '\0';
     return i;
}
/*copy: copy 'from' into 'to'; */
void copy(char to[], char from[])
{
    int i;
    i =0;
    while((to[i] = from[i]) != '\0')
          ++i;
     
}

Write a temp conversion program that uses a function

#include <stdio.h>
float celsius(float fahr);
  
/*print Fahrenheit - Celsius table
  for fahr = 0, 20, ......., 300; floating-point version 
   */
int main(){
    float fahr;
    int lower, upper, step;

    lower =0;          /*lower limit of temperature */
    upper = 300;       /*upper limit */
    step = 20;          /*step size */

    fahr = lower;
    while (fahr <= upper){
          printf("%3.0f %6.1f\n", fahr, celsius(fahr));
          fahr = fahr + step;
    }
}
/*celsius: convert fahr into celsius    */
float celsius(float fahr){
      return (5.0 / 9.0) * (fahr - 32.0);
}

Write a program to print a histogram of the frequencies of different characters in its input.

#include <stdio.h>
#include <ctype.h>

#define    MAXHIST     15          /* max length of histogram */
#define    MAXCHAR      128        /*max different characters */

/*print horizontal histogram freq. of different characters */
int main(){
    int c, i;
    int len;                         /* length of each bar */
    int maxvalue;                    /* maximum value for cc[] */
    int cc[MAXCHAR];                 /* character counters */

    for (i=0 ;i <MAXCHAR; ++i)
           cc[i] = 0;
    while ((c =getchar()) != EOF)
        if (c < MAXCHAR)
               ++cc[c];
    maxvalue = 0;
   for( i = 1; i < MAXCHAR; ++i){
       if(isprint(i))
           printf(" %5d - %c - %5d : ", i, i, cc[i]);
       else
          printf(%5d -  - %5d : ", i, cc[i])
      if (cc[i] > 0){
             if ((len = cc[i] * MAXHIST / maxvalue) <= 0)
                   len = 1;
       }else
            len = 0;
       while (len > 0){
              putchar(' *');
              --len;
       }
        putchar('\n');
   }
}

write a program to print a histogram of the lengths of words in its input.It is easy to draw the histogram with the bars horizontal;a vertical orientation is more challenging.

#include <stdio.h>

#define    MAXHIST  15        /*max length of histogram */
#define    MAXWORD   11       /*max length of a word */
#define     IN        1       /*inside a word */
#define     OUT        0      /*outside a word */

/*print a horizontal histogram              */
int main(){
    int c, i, nc, state;
    int len;                      /*length of each bar */
    int maxvalue;                  /*maximum value for wl[]   */
    int ovflow;                    /*number of overflow words */
    int wl[MAXWORD];               /*word length counters */

     state =OUT;
     nc = 0;                  /*number of chars in a word */
     ovflow =0                /*number of words >= MAXWORDS*/
     for(i =0; i <MAXWORD; ++i)
          wl[i] = 0;
      while((c =getchar()) != EOF){
            if (c == ' ' || c == '\n'  || c =='\t'){
                 state =OUT;
                 if (nc > 0)
                    if (nc < MAXWORD)
                        ++wl[nc];
                    else
                        ++ovflow;
                 nc = 0;
                 }else if (state == OUT){
                      state = IN;
                      nc = 1;           /*beginning of a new word */
                 }else
                      ++nc;             /*inside a word */
            }
             maxvalue = 0;
             for(i = 1; i< MAXWORD; ++i)
                 if (wl[i] > maxvalue)
                        maxvalue = wl[i];
            for( i = 1; i <MAXWORD; ++i){
                printf("%5d - %5d : ", i, wl[i]);
                if (wl[i] > 0) {
                    if ((len = wl[i] = MAXHIST /maxvalue) <= 0)
                          len = 1;
                }else
                     len  = 0;
                while(len > 0){
                      putchar(' * ');
                      --len;
                }
                 putchar('\n');
            }
             if (ovflow > 0)

                 printf("There are %d  words >= %d\n", ovflow, MAX\WORD);
} 


/*
     VERTICAL HISTOGRAM
*/
#include <stdio.h>

#define   MAXHIST        15      /*max length of histogram */
#define   MAXWORD         11     /*max length of a word     */
#define     IN             1         /* inside a word */
#define      OUT           0         /*outside a word */

/*print vertical histogram */
int main(){
      int c, i, j, nc, state;
       int maxvalue;                 /*maximum value for wl[]   */
       int ovflow;                   /*number of overflow words  */
       int wl[MAXWORD];              /*word length counters   */
       state = OUT;
       nc = 0;                       /*number of chars in a word */
       ovflow = 0;                 /*number of words >= MAXWORD   */
       for (i = 0; i < MAXWORD; ++i)
             wl[i] = OUT;
       while ((c = getchar()) != EOF) {
             if (c == ' ' || c == '\n' || c == '\t'){
                 state =OUT;
                 if (nc > 0)
                    if (nc < MAXWORD)
                        ++wl[nc];
                    else
                        ++ovflow;
                 nc = 0;
             }else if (state == OUT) {
                 state = IN;
                 nc = 1;          /*beginning of a new word   */
             }else
                   ++nc;          /*inside a word             */
       }
        maxvalue = 0;
        for(i = 1; i< MAXWORD; ++i)
            if (wl[i] > maxvalue)
                maxvalue  = wl[i];
       for (i = MAXHIST; i > 0; --i){
           for (j = 1; j < MAXWORD; ++j)
                if (wl[j] = MAXHIST /maxvalue >= i)
                     printf(" *");
                else
                     printf("       ");
           putchar('\n');
       } 
       for (i = 1; i <MAXWORD; ++i)
           printf("%4d", i);
       putchar('\n')
       for(i = 1; i <MAXWORD; ++i)
           printf("%4d ", wl[i]);
       putchar('\n');
       if (ovflow > 0)
           printf("There are %d words >= %d\n", ovflow, MAXWORD);      
                 
}

Write a program that prints its input one word per line.

#include <stdio.h>

#define    IN      1    /*inside a word */
#define    OUT     0    /*outside a word */

/*print input one word per line */
int main(){
    int c, state;

     state =OUT;
     while ((c =getchar()) != EOF){
         if (c == ' ' || c == '\n' || c == '\t'){
             if(state == IN ){
                putchar('\n');
                state = OUT;
             }
          }else if (state == OUT){
             state =IN;     /*beginning of word */
             putchar(c);
          }else             /*inside a word */
               putchar(c);
     }
}

Convert word description to declaration.

/*undcl: convert word description to declaration */
main() {
     int type;
     char temp[MAXTOKEN];

     while (gettoken() != EOF){
         strcpy(out, token);
         while((type =gettoken()) != '\n')
             if (type == PARENS || type == BRACKETS)
                 strcat(out, token);
             else if (type == '*') {
                  sprintf(temp, "(*%s)", out);
                  strcpy(out, temp);
             } else if(type == NAME){
                   sprintf(temp, "%s %s", token ,out);
                   strcpy(out, temp);
                 }else
                  printf("invalid input at %s\n", token);
     printf("%s\n", out);
     }
      return 0;
    }

dcl: parse a declarator

#include<stdio.h>
#include <string.h>
#include<ctype.h>

#define MAXTOKEN   100
enum {NAME, PARENS, BRACKETS};
void dcl(void);
void dirdcl(void);

int gettoken(void);
int tokentype;           /*type of last token */
char token[MAXTOKEN];     /*last token string */
char name[MAXTOKEN];      /*identifier name */
char datatype[MAXTOKEN];     /*data type = char, int, etc, */
char out[1000];              /* output string */

main(){  /* convert declaration to words */
    while(gettoken() != EOF){   /* 1st token on line */
          strcpy(datatype, token);   /* is the datatype */
          out[0] = '\0';
          dcl();        /* parse rest of line */
          if (tokentype != '\n')
              printf("syntax error\n");
          printf("%s: %s %s\n", name, out, datatype);
    }
    return 0;
}
int gettoken(void) {/* return next token */
    int c, getch(void);
    void ungetch(int);
    char *p =token;

     while ((c =getch()) == ' ' || c =='\t')
                 ;
     if (c == '(') {
         if ((c =getch()) == ')' ) {
              strcpy(token, "()");
              return tokentype = PARENS;
         }else {
              ungetch(c);
              return tokentype = '(';
          }
     }else if (c == '[' ) {
          for (*p++ =c; (*p++ = getch()) != ']';)
                      ;
          *p = '\0';
           return tokentype = BRACKETS;  
      } else if (isalpha(c)) {
            for (*p++ = c; isalnum(c = getch()); )
                   *p++ = c;
            *p = '\0';
            ungetch(c);
            return tokentype = NAME;
         } else
               return tokentype = c;
}
/*dcl: parse a declarator */
void dcl(void){
     int ns;

     for (ns = 0; gettoken() == '*';) /* count *'s */
          ns++;
     dirdcl();
     while (ns-- > 0)
         strcat(out, " pointer to ");
}
/*dirdcl: parse a direct declarator */
void dirdcl(void){
    int type;

    if (tokentype == '('){  /* (dcl) */
        dcl();
       if (tokentype != ')')
           printf("error: missing )\n");
   } else if (tokentype == NAME)  /* variable name */
          strcpy(name, token);
  else
     printf("error: expected name or (dcl)\n");
  while ((type == PARENS)
        strcat(out, " array");
        strcat(out, token);
        strcat(out, "of"):
}