CSC 270
Homework 3
assigned 22 Oct, due 12 Nov
Modify your previous calculator program (homework 1 or 2) to work with
strings rather than numbers.
You should be able to do this by
writing two functions named "read_string" and "write_string" (or something
like that) and calling them in place of some of the calls to "scanf" and
"printf" in your old program, as well as writing functions named
"add_string", "sub_string", and "mult_string" to do the corresponding
operations on strings.
The user will type an operator (+, -, or *), then two strings, each
enclosed in double-quote marks.
The program will print out the answer, also in double-quote marks,
in a suitably informative format.
Try to include the input strings in the output, as in the
examples below.
(This can be tricky; if you don't get it to work,
turn in the program without the input strings repeated in the output.)
So, you may ask, what do "+", "-", and "*" mean for strings?
For the purposes of this program,
- the "sum" of two strings is their concatenation.
Example:
panther% hw3
+ "abc def" "ghij"
"abc def" + "ghij" = "abc defghij"
- the "difference" of two strings is the first one, with all
characters that appear in the second one removed.
Example:
panther% hw3
- "antidisestablishmentarianism" "resx"
"antidisestablishmentarianism" - "resx" = "antiditablihmntaianim"
- the "product" of two strings is a string whose length is the product
of the lengths of the two strings, and whose characters are determined
as follows:
Form a two-dimensional array of characters, with number of rows equal to
the length of the first string and number of columns equal to the length
of the second string.
For each position [i,j] in the array, set the [i,j] element of the array
to be the bitwise OR of the i-th character in the first string with the
j-th character in the second string.
The answer is the concatenation of the characters in the first row,
those in the second row, and so on through the last row.
(Or, for extra credit, write out each row separately in a nice
format.)
Example:
panther% hw3
* "aB9" "r E"
"aB9" * "r E" = "saerbG{)}"
panther% hw3_extra_credit
* "aB9" "r E"
"sae"
"aB9" * "r E" = "rbG"
"{)}"
The grading criteria are roughly the same as for homework 1.
I recommend working on the parts of the program in the following order:
- write, test, and debug functions to read and print a string.
(To test this, have each of your operators return the string "xyz"
or something like that regardless of what operands you gave them.)
- write, test, and debug the "add_string" function, which
concatenates strings.
- write, test, and debug the "sub_string" function, which deletes all
characters in the second from the first.
- write, test, and debug the "mult_string" function, which does the
weird stuff with the two-dimensional array and the bitwise OR.
Last modified:
Tue Oct 22 11:47:10 EDT 1996
Stephen Bloch / sbloch@boethius.adelphi.edu