Software 1 - getline, capitalize, ring (3)


administrivia

Web Page - http://www.cs.tau.ac.il/~efif/courses/Software1_Spring_03


Character Input and Output


getchar


getchar example


getchar return type


EOF


terminating a program


shorter getchar example


getline

#include <stdio.h>

/*! getline() reads one line from standard input and copies it to line array
 * (but no more than max chars).
 * It does not place the terminating \n in line array.
 * Returns line length, or 0 for empty line, or EOF for end-of-file.
 */
int getline(char line[], int max)
{
  int nch = 0;
  int c;
  max = max - 1;			/* leave room for '\0' */

  while ((c = getchar()) != EOF) {
    if (c == '\n')
      break;

    if (nch < max) {
      line[nch] = c;
      nch = nch + 1;
    }
  }

  if (c == EOF && nch == 0)
    return EOF;

  line[nch] = '\0';
  return nch;
}

Shell prompt


Capitalizing first character


Changing the csh prompt


ring

#include <stdio.h>

/*!
 * constants
 */

#define  WIDTH      60
#define  HEIGHT     60
#define  CENTER_X   30
#define  CENTER_Y   30
#define  R          20
#define  THRESHOLD  R

/*!
 * main
 */
int main()
{
  int x, y, val;

  for (y = 0; y < HEIGHT; y++) {
    for (x = 0; x < WIDTH; x++) {
      val = (y - CENTER_Y) * (y - CENTER_Y) +
        (x - CENTER_X) * (x - CENTER_X) - R * R;

      if (abs(val) < THRESHOLD)
        printf("%c", '#');
      else
        printf(" ");
    }
    printf("\n");
  }
  return  0;
}  

practices prev top next


Maintained by Efi Fogel. Last modified: March 07 2003.