My Own `tr`
Last modified
Background
Complete the motr program
Here is what we completed in class:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int c;
char *pch;
if ( argc < 3 ) {
fprintf(stderr, "Usage: %s SET1 SET2\n", argv[0]);
exit(-1);
}
while ((c = getchar()) != EOF) {
pch = strchr(argv[1], c);
if ( pch != NULL ) {
//TODO: replace the character found with
//the corresponding char in SET2
}
putchar(c);
}
}
The goal is to create a program that accepts two command line arguments from the user: SET1
and SET2
. The program will read characters from /standard input/. Any characters found that are contained in SET1
will be replaced with the equivalent (same index) in SET2
. For now assume that SET1
and SET2
are the same length.
Hint: The index of the character relative to SET1
can be found by calculating the difference between the pointer address returned by strchr
and the pointer address of the first character in SET1
.
Test your program
Try changing all vowels to the letter ‘x’:
$ ./motr aeiou xxxxx
:Hello World
Hxllx Wxrld
:^d
Or all upper case A
, B
and C
to lower case:
$ ./motr ABC abc
:THE CAT SAT ON THE MAT
THE caT SaT ON THE MaT
^d
Or swap all c
s and m
s
$ ./motr mc cm
:the cat sat on the mat
the mat sat on the cat
:the mat sat on the cat
the cat sat on the mat
:^d
Commit your changes
You will first have to add the modified motr.c
to the staging area:
$ git add motr.c
then commit the changes
$ git commit
Since we aren’t using the -m
option here git
will open your default editor so that you can enter a commit message. The message could be something like
accept SET1, SET2 command line arguments
Usage: ./motr SET1 SET2
all characters in the input stream found
in SET1 will be replaced with the character
at the same index in SET2.
No error checking is performed for
index out of bound errors.
Once you have entered your message, save and quit from the editor to allow git to record the commit (if your default editor is vim
then the key sequence would be <Esc>
to get into COMMAND mode and then :wq<Enter>
).
Submission
The source files should exist in their own git repository, if you change to the directory containing your source files and run ls -a
you should see a directory named .git
. If not, run git init
to initialize a git repository in the current directory. You should only run git init
once for each new project.
Push your git repository to the remote at git@ece2524.ece.vt.edu:USER/motr.git
where USER
is your git user name.
If you have initialized a new repo but have not added a remote yet:
$ git remote add origin git@ece2524.ece.vt.edu:USER/motr.git
where is your git user name.
If you have already added a remote named origin
, but the URL is incorrect, replace add
with set-url
in the above command. You can always check that remotes you have added by running git remote -v
.
Remember, if this is the first time pushing to a new remote you need to specify a destination branch (usually `master`). Using the `-u` option will save this default destination for future pushes.
$ git push -u origin master
Testing
Feature repo path: features/motr
The following features will be tested using cucumber:
Feature: My Own tr
Background:
Given the executable path includes the current directory
And a file named "jump.txt" with:
"""
The Quick Brown Fox Jumped Over The Lazy Dog.
"""
Scenario: No Arguments
When I run the shell command "motr"
Then the stderr should contain "Usage: motr SET1 SET2"
Scenario: Letter Swap
When I run `motr mc cm` interactively
And I type "The cat sat on the mat"
And I close the stdin stream
Then the stdout should contain exactly:
"""
The mat sat on the cat
"""
Scenario: Input Redireciton
When I run the shell command "motr QBF qbf <jump.txt"
Then the stdout should contain exactly:
"""
The quick brown fox Jumped Over The Lazy Dog.
"""
You can run the tests manually with
$ cucumber /usr/share/features/motr
when logged in to your shell account. This command assumes your current working directory is your project directory.