CSci 170: Analysis of Programming Languages

A short C programming assignment

The echo program is a standard C language example program. It prints out at the console the arguments to its right. For instance, if you enter the command

$ echo My foot!
then the computer prints out on the next line
My foot!

There are four versions of echo below: echo1.c, echo2.c, echo3.c, and echo4.c. We'll discuss them in class.

// echo1.c.  A simple echo program that uses indices in arrays
int main (int argc, char *argv[]) {
  int i,j;
  for (i = 1; i < argc; i++) {
    for (j = 0; argv[i][j] != '\0'; j++)
      putchar(argv[i][j]);
    putchar(' ');
  }
  putchar('\n');
  return 0;
}

// echo2.c.  Like echo1.c, but uses formatted output
int main (int argc, char *argv[]) {
  int i,j;
  for (i = 1; i < argc; i++)
    printf("%s ",argv[i]);
  printf("\n");
  return 0;
}

// echo3.c.  Like echo2.c, but uses pointers instead of array indices
int main (int argc, char *argv[]) {
  int i,j;
  while (*++argv)
    printf("%s ",*argv);
  printf("\n");
  return 0;
}

// echo4.java is like echo1.java, except everything is done with pointers
int main (int argc, char **argv) {
  char **p, *q;
  for (p = argv+1; *p != 0; p++) {
    for (q = *p; *q != '\0'; q++)
      putchar(*q);
    putchar(' ');
  }
  putchar('\n');
  return 0;
}
Here's how to compile and run one of them, echo1.c. At the prompt $, enter gcc to run the gnu c compiler with the name of the file you want to compile. When there are no syntax errors, the executable image a.out is created. You can rename it echo (or anything else). Then you can run echo as a program with paramters as done below.
$ gcc echo1.c
$ mv a.out echo1
$ echo1 Lincoln was once president.
Lincoln was once president.
$ echo1 echo
echo
$
See echos.txt for a couple other runs.

Your assignment is to create, test, and run a program ohce to print the parameters in reverse, and each parameter should be reversed. You can call your program ohce.

$ohce Lincoln was once president.
.tnediserp ecno saw nlocniL
$ohce Madam Im Adam
madA mI madaM
$ohce ohce
echo
$