print longest input line;specialized version.

#include <stdio.h>

#define MAXLINE  1000  /*maximum input line size */

int max;               /*maximum length seen so far */
char line[MAXLINE];    /*current input line */
char longest[MAXLINE]; /*longest line saved here */

int getline(void);
void copy(void);

/*print longest input line;specialized version */
main()
{
   int len;
   extern int max;
   extern char longest[];

   max = 0;
   while ((len =getline()) > 0)
       if (len > max) {
           max = len;
           copy();
       }
   if (max > 0)    /*there was a line */
      printf("%s", longest);
   return 0;
}

/*getline: specialized version */
int getline(void)
{
   int c, i;
   extern char line[];

  for(i =0; i < MAXLINE -1 && (c=getchar()) != EOF && c!= '\n'; ++i)
      line[i] = c;
  if (c == '\n') {
     line[i] = c;
     ++i;
  }
  line[i] = '\0';
  return i;
}
/*copy: specialized version */
void copy(void)
{
   int i;
   extern char line[], longest[];

   i = 0;
   while ((longest[i] = line[i]) != '\0')
          ++i;
}

Read a set of text lines and print the longest.

#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 */
main()
{
   int len;                          /*current line length */
   int max;                         /*maximum length seen so far */
   char longest[MAXLINE];           /*longest line saved here */

   max = 0;
   while ((len = getline(line, MAXLINE)) > 0)
      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;

   for (i =0; i<lim - 1 && (c =getchar()) !=EOF && c!='\n';++i)
        s[i] = c;
   if (c =='\n') {
      s[i] =c;
      ++i;
   }
   s[i] ='\0';
   return i;
}

/*copy : copy 'from' into 'to';assume to is big enough */
void copy(char to[], char from[])
{
   int i;

   i = 0;
   while((to[i] =from[i]) != '\0')
        ++i;
}

power function;

#include <stdio.h>
int power(int m ,int n);
/*test power function */
main()
{
   int i;

    for( i = 0; i< 10;++i)
       printf("%d %d %d\n", i, power(2, i), power(-3, i));
    return 0;
}
/*power; raise base to n-th power; n>= 0 */
int power(int base, int n)
{
   int i, p;
   p = 1;
   for (i = 1; i<= n; ++i)
        p = p * base;
   return p;
}

}

count digits, white space, others

#include <stdio.h>

/*count digits, white space, others */

main()

{

int c, i , nwhite, nother;

int ndigit[10];

nwhile=nother = 0;

for (i = 0; i< 10;++i)

ndigit[i] = 0;

while ((c = getchar()) != EOF)

if (c >= ‘0’ && c <= ‘9’)

++ndigit[c – ‘0’];

else if (c == ‘ ‘ || c == ‘\n’ || c == ‘\t’)

++nwhite;

else

++nother;

printf(“digits =”);

for (i = 0; i < 10; ++i)

printf(” %d”, ndigit[i]);

printf(“, white space = %d, other = %d\n”, nwhite, nother);

}

word count.

#include <stdio.h>

#define IN 1 /*inside a word */

#define OUT 0 /*outside a word */

/*count lines, words, and characters in input */

main(){

int c, nl, nw,nc, state;

state = OUT;

nl = nw =nc = 0;

while ((c =getchar()) != EOF){

++nc;

if (c == ‘\n’)

++nl;

if (c == ‘ ‘ || c == ‘\n’ || c == ‘\t’)

state = OUT;

else if (state == OUT) {

state = IN;

++nw;

}

}

printf(“%d %d %d\n”, nl , nw, nc);

}