implement a simplistic version of Linux tr (translate) command. Your program:

computer science

Description

Question 1

 

In F1.c implement a simplistic version of Linux tr (translate)

command. Your program:

 

 -expects 2 command-line arguments (CLAs), set1 and set2

 -reads characters from stdin, and writes them to stdout,

  replacing characters from set1 by those of set2 (like tr).

 

 -Note:

  -sets are alphanumeric characters of same length e.g.:

   ./a.out abc x3Q

  -if not 2 CLAs, or if set1 and set2 not same length, or set1

   not alphanumeric, or set2 not alphanumeric, print

     "BAD INPUT" on stdout and exit. (Note: stdout, not stderr!)

 

 -do NOT implement tr's options or "interpreted sequences", as

  defined in the tr man page (e.g., -d, -s, '\n', [:alnum:])

 

 -Example:

 

>cat infile

m fnd

vibux

>gcc F1.c

>./a.out mfvb CaLn <infile

C and

Linux

> 

 

 -TEST YOUR PROGRAM. Its output should match tr's. E.g.,

 

>tr mfvb CaLn <infile

C and

Linux

> 

 

Question 2

 

Consider the following memory representation, similar to

the course notes:

 

    100  101  102  103   ...  200  ... 308  ... 400

   --------------------------------------------------

...|'a' |'c' |'e' |'\0'| ... |100 |...|102 |...|200 |...

   --------------------------------------------------

                               X        Y        Z

 

Copy the program below into file F2.c and replace all four

AAAA so that the program corresponds to the above memory

representation.

 Note:

  -Each AAAA is replaced with a different code fragment.

  -Your actual addresses will differ, but equal addresses

   above should still be equal in your program, i.e.,

    -the value of X should be the same as the address where

     'a' is located in memory.

    -the value of Y should be the same as the address where

     'e' is located in memory.

    -the value of Z should be the same as the address of X

    -the last two AAAA must be replaced with code which uses

     variable Z to obtain the value of X[1].

 

 

This results in zero on this question:

Any modifications to the given code other than replacing

AAAA.  e.g., do not add/remove spaces.

 

 

When your program runs, it must print:

X=ace

Y=e

X[1]=c

 

 

#include <stdio.h>

int main (void) {

  char *X = "AAAA";    //2 marks

  char *Y = AAAA;      //2 marks

  char **Z = &X;

  printf("X=%s\n",X);

  printf("Y=%s\n",Y);

  //next printf MUST use Z to print value of X[1]

  printf("X[1]=%c\n",AAAA Z AAAA));  //4 marks

}


Related Questions in computer science category